note 97110 added to language.oop5.overloading
| From: | f4bi0_ at hotmail dot com | Date: | Thu, 01 Apr 2010 21:12:21 +0000 |
| Subject: | note 97110 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167632@lists.php.net to get a copy of this message | ||
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
----
Server IP: 69.147.83.197
Probable Submitter: 189.30.251.63
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/97110
Del: integrated -- https://master.php.net/note/delete/97110/integrated
Del: useless -- https://master.php.net/note/delete/97110/useless
Del: bad code -- https://master.php.net/note/delete/97110/bad+code
Del: spam -- https://master.php.net/note/delete/97110/spam
Del: non-english -- https://master.php.net/note/delete/97110/non-english
Del: in docs -- https://master.php.net/note/delete/97110/in+docs
Del: other reasons-- https://master.php.net/note/delete/97110
Reject -- https://master.php.net/note/reject/97110
Search -- https://master.php.net/manage/user-notes.php