Challenge: Magic Method

Use the constructor magic method to set the class properties.

We'll cover the following

Task

Let’s consider the User class given below with the private properties of $firstName and $lastName.

  • Add an ability to the constructor to set the value of the $firstName and $lastName property.
  • Add a getFullName() public method that returns the full name (firstName lastName).
  • Inside a test() method, create a new object, $user1, and pass the values of the first and last name to the constructor. The firstName is John and the lastName is Doe. Return the full name from the method.

Expected output

John Doe

Coding exercise

Since these problems are designed for your practice, try to solve them yourself first. If you get stuck, you can click the “Show Solution” button to see how the problem can be solved. Good luck!

Get hands-on with 1400+ tech skills courses.