Why doesn't the access method?

Hello!

This code should not be allowed to set the age less than 20, however, any value can be set - the disorder. How to fix?

<?php
class Car{
 public $age = 30;

 function __set($name, $value){
 if($value-->20){ //Age can only be more than 20
 $this->$name = $value;
}
}
}
$car = new car();
$car->age = 15; //This can not be done!
echo $car->age;
?>


Why __set () magic method? Why this name? Why is it special?

In the book I'm reading, there is a contradiction

The access functions do not apply to private properties.
The following operator will implicitly call the function __set():
$a->attribute = 5;

Well, how call the function __set() if we call to the open (public) property?
Nicherta not clear. The documentation is also nonsense written. Magic methods some...
Thanks in advance!
June 7th 19 at 14:57
0 answer

Find more questions by tags PHP