⊗ppOpMgPrm 94 of 107 menu

Practical Application of Magic Methods in OOP in PHP

You will learn the practical application of the __set method on your own by solving the following task:

Let's say we have the following User class with getters and setters for properties:

<?php class User { private $name; private $age; public function getName() { return $this->name; } public function setName($name) { if ($name != '') { $this->name = $name; } } public function getAge() { return $this->age; } public function setAge($age) { if ($age >= 0 and $age <= 70) { // check age $this->age = $age; } } } ?>

Rewrite the code of this class so that instead of getters and setters, the magic methods __get and __set are used.

azbydeenesfrkakkptruuz