note 109401 modified in function.get-object-vars by danbrown

From: Date: Mon, 16 Jul 2012 15:19:39 +0000
Subject: note 109401 modified in function.get-object-vars by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189178@lists.php.net to get a copy of this message
Since there&#39;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....&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;&#13;&#10;function get_private_properties($obj, $inside=false)&#13;&#10;{&#13;&#10; $obj_dump = print_r($obj, 1);&#13;&#10; preg_match_all(&#39;/^\\s+\\[(\\w+):private\\]/m&#39;, $obj_dump, $matches);&#13;&#10; if ($inside)&#13;&#10; {&#13;&#10; $output = array();&#13;&#10; foreach ($matches[1] as $property)&#13;&#10; {&#13;&#10; $output[$property] = $obj-&#62;$property;&#13;&#10; return $output;&#13;&#10; }&#13;&#10; }&#13;&#10; else return $matches[1];&#13;&#10;}&#13;&#10;&#13;&#10;?&#62;&#13;&#10;&#13;&#10;So if you run it with the optional second paramter missing you&#39;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.&#13;&#10;&#13;&#10;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&#39;d only advise to do that for singletons since you may get errors if there are any private properites in parents/children. --was-- 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. http://php.net/manual/en/function.get-object-vars.php

« previous php.notes (#189178) next »