note 97110 deleted from language.oop5.overloading by cmb
| From: | cmb@php.net | Date: | Sun, 03 Mar 2019 11:38:44 +0000 |
| Subject: | note 97110 deleted from language.oop5.overloading by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210312@lists.php.net to get a copy of this message | ||
Note Submitter: f4bi0_ at hotmail dot com
----
TRICKY CHALLENGE: is there a way to check if $object->variable exists without automatically
triggering the functionality of __get in case the variable doesn't exist?
TRADITIONAL WAY:
<?php
$object = new someClass(); // imagine someClass uses the Magic Method "__get"
if($object->variable){
// do something
} else {
// "__get" has inevitably been triggered :-(
}
?>
Ok, above we have the conventional way to check if a variable exists inside the object, the problem
is that sometimes we DON'T WANT TO TRIGGER __GET in case the statement returns false!
ALTERNATIVE WAY:
<?php
if(array_key_exists( 'variable', get_object_vars($object) ) ){
// do something
} else {
// "__get" has no idea what is going on, i think it is still asleep :-)
}
?>
you can even turn this into a function or something