note 109401 added to function.get-object-vars

From: Date: Fri, 13 Jul 2012 21:27:53 +0000
Subject: note 109401 added to function.get-object-vars
Groups: php.notes 
Request: Send a blank email to php-notes+get-189130@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. http://www.rxgs.com 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. ---- Server IP: 69.147.83.197 Probable Submitter: 85.17.93.6 ---- Manual Page -- http://www.php.net/manual/en/function.get-object-vars.php Edit -- https://master.php.net/note/edit/109401 Del: integrated -- https://master.php.net/note/delete/109401/integrated Del: useless -- https://master.php.net/note/delete/109401/useless Del: bad code -- https://master.php.net/note/delete/109401/bad+code Del: spam -- https://master.php.net/note/delete/109401/spam Del: non-english -- https://master.php.net/note/delete/109401/non-english Del: in docs -- https://master.php.net/note/delete/109401/in+docs Del: other reasons-- https://master.php.net/note/delete/109401 Reject -- https://master.php.net/note/reject/109401 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#189130) next »