Challenge: Data Handling Using the PDO Extension and Abstract Methods

A hands-on exercise to test your knowledge of PHP 8 best practices.

Task 1

  • The code should follow strict typing.

  • Implement a class that implements the abstract method of the trait that performs addition.

  • Define a trait that will be used in another class. This trait should contain two methods:

    • The first method is an abstract method representing a generic mathematical operation.

    • The other is not abstract and is implemented within the trait. It should be a wrapper around the first method.

  • Finally, echo the results to the console.

Instructions

  1. Define the addition math operation in the Calculator class.

  2. Modify the Calculator class to implement the performOperation method, which will perform the specific math operation. In this case, you’ll add $b and $a.

  3. Create an instance of the Calculator class.

  4. Use the calculate method to perform the specific math operation with the provided numbers (10.5 and 5.2 in this case).

  5. Print the result to the screen.

Coding playground

Get hands-on with 1400+ tech skills courses.