Search⌘ K
AI Features

Solution Review: Magic Method

Explore the use of PHP magic methods by examining a User class with private properties and a constructor. Understand how to set and retrieve data using methods like getFullName and test object creation and data handling in OOP context.

We'll cover the following...

Solution

PHP
<?php
class User {
private $firstName;
private $lastName;
public function __construct($firstName,$lastName) {
$this -> firstName = $firstName;
$this -> lastName = $lastName;
}
public function getFullName() {
return $this -> firstName . ' ' . $this -> lastName;
}
}
function test()
{
$user1 = new User("John", "Doe");
return $user1 -> getFullName();
}
echo test();
?>

Explanation

  • Line 2: We create a User class with two private properties, $firstName and $lastName.
  • Line 6: We add a
...