arianhojat Posted February 13, 2007 Share Posted February 13, 2007 had question about References in the php manual.... "When assigning an already created instance of an object to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A new instance of an already created object can be made by cloning it." class SimpleClass { // member declaration public $var = 'a default value'; // method declaration public function displayVar() { echo $this->var; } } $instance = new SimpleClass(); $assigned = $instance; $reference =& $instance; $instance->var = '$assigned will have this value'; $instance = null; // $instance and $reference become null var_dump($instance); var_dump($reference); var_dump($assigned); //prints out Now the manual seems to suggest u have to manually clone an object if you want to copy it to another variable by using clone() in php5, and just assigning a variable to an object, that it will make it a reference and not a copy. Is that really true? cause it seems to suggest u have to manually have to add &= for reference to work (unless use 'new' operator which automatically adds a &=). $assigned seems to have cloned its own copy of $instance without using clone(). Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 They changed this between 4 & 5, which version are you using, and which are you reading the docs for? Quote Link to comment Share on other sites More sharing options...
arianhojat Posted February 13, 2007 Author Share Posted February 13, 2007 version 5 for both Quote Link to comment Share on other sites More sharing options...
arianhojat Posted February 14, 2007 Author Share Posted February 14, 2007 bump Quote Link to comment Share on other sites More sharing options...
arianhojat Posted February 14, 2007 Author Share Posted February 14, 2007 uh oh, the dreaded "double bump". dont want to open a new topic, although i imagine bumps are frowned upon so maybe i will do that if no answer. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.