note 72075 added to function.get-object-vars
| From: | Leon at osu1 dot php dot net | Date: | Mon, 01 Jan 2007 05:44:31 +0000 |
| Subject: | note 72075 added to function.get-object-vars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121160@lists.php.net to get a copy of this message | ||
I was frustrated while writing unit tests that I could not
'get at' the private properties of the objects I was running
tests on with the built-in version of get_object_vars(), so I
came up with the following.
It's ugly and hacky, but sometimes you really do need a
bigger hammer... :-)
<?php
/**
Like the PHP function get_object_vars(), but gets access
to private properties too.
@param mixed Object
@return array Returns an associative array of object properties
*/
function getObjectVars($obj)
{
// Build PHP code for an array declaration from var_export() output
$str = var_export( $obj, true );
$str = preg_replace('/^.*set_state\(/', '$properties = ', $str);
$str = preg_replace('/\)\)$/', ');', $str);
eval($str);
// This variable was defined in the code that we ran in eval()
return $properties;
}
?>
----
Server IP: 202.89.36.242
Probable Submitter: 203.173.146.182 (proxied: unknown)
----
Manual Page -- http://www.php.net/manual/en/function.get-object-vars.php
Edit -- https://master.php.net/note/edit/72075
Del: integrated -- https://master.php.net/note/delete/72075/integrated
Del: useless -- https://master.php.net/note/delete/72075/useless
Del: bad code -- https://master.php.net/note/delete/72075/bad+code
Del: spam -- https://master.php.net/note/delete/72075/spam
Del: non-english -- https://master.php.net/note/delete/72075/non-english
Del: in docs -- https://master.php.net/note/delete/72075/in+docs
Del: other reasons-- https://master.php.net/note/delete/72075
Reject -- https://master.php.net/note/reject/72075
Search -- https://master.php.net/manage/user-notes.php