At work I was told to add in the magic method __isset() (which is technically done by overloading the function…). Essentially it lets you call isset() on private and protected variables in your classes:
/** As of PHP 5.1.0 */ class MyClass{ private $private; public $public; protected $protected; public function __isset($name) { echo "Is '$name' set? "; return isset($this->name); } } $myClass = new MyClass(); echo isset($myClass->private); #Is 'private' set? false echo isset($myClass->protected); #Is 'protected' set? false echo isset($myClass->public); #false |
This function will be called whenever the variable is unreachable by a normal isset call (private and protected variables)
Nice and easy if you know exactly what you want. Unfortunately sometimes people have the habit of adding underscores to the start of their variables, but not always. So to make this work, I decided it would be a good idea to make it check to see if the variable was there, or if maybe there should have been an underscore at the begining. So it became:
/** As of PHP 5.1.0 */ class MyClass{ private $_private; public $public; protected $protected; public function __isset($name) { if(isset($this->$name)){ return true; } $name = "_".$name; return isset($this->$name); } } $myClass = new MyClass(); echo isset($myClass->_private); #Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 134136 bytes) in isset_r.php on line 20 |
Curious, so I changed it a bit to see what was actually happening in there…
__private___private____private_____private______private_______private________private_________private
__________private___________private____________private_____________private______________private___
____________private________________private_________________private__________________private______
_____________private____________________private_____________________private______________________
private_______________________private________________________private_________________________priv
ate__________________________private___________________________private__________________________
__private_____________________________private______________________________private_______________
________________private________________________________private_________________________________
private__________________________________private___________________________________private______
______________________________private_____________________________________private______________
________________________private_______________________________________private__________________
______________________private_________________________________________private__________________
________________________private___________________________________________private______________
______________________________private_____________________________________________private______
________________________________________private_______________________________________________
private________________________________________________private
Fun stuf… the lesson is: name things a standard way and you are good to go, if you don’t it will screw things up royally (but it might make a pretty design).