note 38482 added to function.get-object-vars

From: Date: Tue, 23 Dec 2003 17:49:51 +0000
Subject: note 38482 added to function.get-object-vars
Groups: php.notes 
Request: Send a blank email to php-notes+get-62382@lists.php.net to get a copy of this message
Please note that you cannot affect the object via the array values...in other words, the returned array does not contain references to the values within the object, but copies. If you are making an object inspector or editor, this is not good enough. So I made the following methods: METHODS: function &getVar($obj, $name) { $expr="\$prop=&\$obj->$name;"; eval($expr); return $prop; } function &getObjectVars($obj) { $result=array(); $vars=get_object_vars($obj); foreach ($vars as $var => $value) { $result[$var]=&getVar(&$obj, $var); } return $result; } [NOTE: You must pass in a reference to an object, not an object. Sorry if this offends PHP'ers, but the distinction of pass-by-value and copy-on-assignment drives me batty (compared to Python, Java, Smalltalk), so I make all my functions pass by value, and force myself to pass in a reference to keep track of what is happening under the hood.] EXAMPLE: class Bob { function Bob() { $this->thing=13; $this->other="whatever"; } var $thing; var $other; } $obj=&new Bob(); # NOTE: Passing in a reference! $props=getObjectVars(&$obj); $props["thing"]=-11; var_dump($obj); RESULTS: object(bob)(2) { ["thing"]=> &int(-11) ["other"]=> &string(8) "whatever" } ---- Manual Page -- http://www.php.net/manual/en/function.get-object-vars.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38482 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38482&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38482&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#62382) next »