PHP 5 introduces the magic method __get () and __set (). In my opinion, this is a shortcut for writing each getter and setter member;
<?php
class Person {
private $firstname;
private $lastname;
function __set($var, $name) {
$this->$var = $name;
}
function __get($var) {
return $this->$var;
}
}
$person = new Person();
$person->firstname = "Tom";
$person->lastname = "Brady";
echo $person->firstname . " " . $person->lastname;
?>
My question is, this is just like public member variables.
class Person {
public $firstname;
public $lastname;
}
Isn't that against the principles of OOP? And what's the point?
source
share