note 58835 added to function.get-object-vars

From: Date: Thu, 17 Nov 2005 11:40:55 +0000
Subject: note 58835 added to function.get-object-vars
Groups: php.notes 
Request: Send a blank email to php-notes+get-98702@lists.php.net to get a copy of this message
Since there's no apparent means of obtaining all the *private* properties in an object I wrote a little function to do it. Built in support would be much more efficient since mine uses a preg_ search to do this.... <?php function get_private_properties($obj, $inside=false) { $obj_dump = print_r($obj, 1); preg_match_all('/^\s+\[(\w+):private\]/m', $obj_dump, $matches); if ($inside) { $output = array(); foreach ($matches[1] as $property) { $output[$property] = $obj->$property; return $output; } } else return $matches[1]; } ?> So if you run it with the optional second paramter missing you'll just get an array of the variable names that are private inside the class. This is the only option if you are not inside the actual object and the object has no private properties inherited. If you run it with the second parameter set to true you will get an associative array with the properties and their corresponding values. I'd only advise to do that for singletons since you may get errors if there are any private properites in parents/children. ---- Manual Page -- http://www.php.net/manual/en/function.get-object-vars.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58835 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58835&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58835&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58835&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58835&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58835&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58835&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58835&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58835&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98702) next »