note 75330 added to function.get-object-vars

From: Date: Thu, 24 May 2007 06:13:16 +0000
Subject: note 75330 added to function.get-object-vars
Groups: php.notes 
Request: Send a blank email to php-notes+get-126863@lists.php.net to get a copy of this message
To follow the code of d11wtq (enquiries AT chriscorbyn.co.uk). I did this function to inspect all properties(public, private, protected) of object. <?php header("content-type: text/plain"); // Classes to test! class OtherClass { private $privateVarOtherClass = 11; } class MyClass extends OtherClass { protected $protectedVar = "some"; public $publicVar = "nk"; private $privateVar = "algo"; var $oldStyle; } // Dangerous function function get_properties($obj, $values=false) { $obj_dump = print_r($obj, 1); $matches = array(); preg_match_all('/^\s+\[(\w+).*\] => (\w*)/m', $obj_dump, $matches); if ($values) { $output = array(); foreach ($matches[1] as $key => $property) { $output[$property] = $matches[2][$key]; } return $output; } else { return $matches[1]; } } $instance = new MyClass(); echo "Properties\n"; print_r(get_properties($instance)); echo "Properties and values\n"; print_r(get_properties($instance, true)); ?> Note: remember that static properties are not visible to the object. By that not supported to them ---- Server IP: 190.74.254.173 Probable Submitter: 200.109.153.207 ---- Manual Page -- http://www.php.net/manual/en/function.get-object-vars.php Edit -- https://master.php.net/note/edit/75330 Del: integrated -- https://master.php.net/note/delete/75330/integrated Del: useless -- https://master.php.net/note/delete/75330/useless Del: bad code -- https://master.php.net/note/delete/75330/bad+code Del: spam -- https://master.php.net/note/delete/75330/spam Del: non-english -- https://master.php.net/note/delete/75330/non-english Del: in docs -- https://master.php.net/note/delete/75330/in+docs Del: other reasons-- https://master.php.net/note/delete/75330 Reject -- https://master.php.net/note/reject/75330 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126863) next »