note 72075 deleted from function.get-object-vars by nlopess

From: Date: Mon, 01 Jan 2007 10:37:10 +0000
Subject: note 72075 deleted from function.get-object-vars by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-121168@lists.php.net to get a copy of this message
Note Submitter: Leon Reason: bad code ---- 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; } ?>

« previous php.notes (#121168) next »