note 112046 added to function.isset

From: Date: Thu, 25 Apr 2013 23:32:36 +0000
Subject: note 112046 added to function.isset
Groups: php.notes 
Request: Send a blank email to php-notes+get-194630@lists.php.net to get a copy of this message
Note that isset() is not recursive as of the 5.4.8 I have available here to test with: if you use it on a multidimensional array or an object it will not check isset() on each dimension as it goes. Imagine you have a class with a normal __isset and a __get that fatals for non-existant properties. isset($object->nosuch) will behave normally but isset($object->nosuch->foo) will crash. Rather harsh IMO but still possible. <?php class FatalOnGet { // pretend that the methods have implementations that actually try to do work // in this example I only care about the worst case conditions public function __get($name) { echo "(getting {$name}) "; // if property does not exist { echo "Property does not exist!"; exit; // } } public function __isset($name) { echo "(isset {$name}?) "; // return whether the property exists return false; } } $obj = new FatalOnGet(); // works echo "Testing if ->nosuch exists: "; if (isset($obj->nosuch)) echo "Yes"; else echo "No"; // fatals echo "\nTesting if ->nosuch->foo exists: "; if (isset($obj->nosuch->foo)) echo "Yes"; else echo "No"; // not executed echo "\nTesting if ->irrelevant exists: "; if (isset($obj->irrelevant)) echo "Yes"; else echo "No"; ?> Testing if ->nosuch exists: No Testing if ->nosuch->foo exists: Property does not exist! Uncomment the echos in the methods and you'll see exactly what happened: Testing if ->nosuch exists: (isset nosuch?) No Testing if ->nosuch->foo exists: (getting nosuch) Property does not exist! On a similar note, if __get always returns but instead issues warnings or notices then those will surface. ---- Server IP: 69.147.83.199 Probable Submitter: 173.160.229.61 ---- Manual Page -- http://php.net/manual/en/function.isset.php Edit -- https://master.php.net/note/edit/112046 Del: integrated -- https://master.php.net/note/delete/112046/integrated Del: useless -- https://master.php.net/note/delete/112046/useless Del: bad code -- https://master.php.net/note/delete/112046/bad+code Del: spam -- https://master.php.net/note/delete/112046/spam Del: non-english -- https://master.php.net/note/delete/112046/non-english Del: in docs -- https://master.php.net/note/delete/112046/in+docs Del: other reasons-- https://master.php.net/note/delete/112046 Reject -- https://master.php.net/note/reject/112046 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194630) next »