phant0m Posted May 25, 2010 Share Posted May 25, 2010 Hi Let's assume I have to Classes, A and B, instances $a and $b respectively. Now, when I create B, I pass a member of A as a reference to B. Then, I do the following: $b->member =& $referenced_member_of_a; This works as expected, it $b->member now "points" to the member from $a; Now, I want to destroy that reference, so that $b->member is independent of the (not yet previously) referenced member of $a. As far as I know, the only possibility to break a reference, is using unset and then reinitializing the variable. With normal variables, this works as expected, however, if I use this on a class member, I get an error as soon as I want to assign something the same member again. unset($b->member); $b->member = "independant of $a"; Will result in a warning, since I'm setting a property that doesn't exist anymore. Is there any other way to break the reference? A way I've thought of is this: $var = "independant of $a"; $b->member =& $var; unset($var); But surely, there must be a more elegant way. Quote Link to comment https://forums.phpfreaks.com/topic/202808-unset-reference-on-php-class-member/ Share on other sites More sharing options...
ignace Posted May 25, 2010 Share Posted May 25, 2010 $var = null; Quote Link to comment https://forums.phpfreaks.com/topic/202808-unset-reference-on-php-class-member/#findComment-1062977 Share on other sites More sharing options...
phant0m Posted May 25, 2010 Author Share Posted May 25, 2010 yeah, that would just make the unset unnecessary, I simply used the string for the example. But is there no other possibility to kill the reference instead of creating a new one? Quote Link to comment https://forums.phpfreaks.com/topic/202808-unset-reference-on-php-class-member/#findComment-1063239 Share on other sites More sharing options...
Daniel0 Posted May 25, 2010 Share Posted May 25, 2010 Why are you creating a reference in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/202808-unset-reference-on-php-class-member/#findComment-1063243 Share on other sites More sharing options...
phant0m Posted May 25, 2010 Author Share Posted May 25, 2010 Because I want them to share the same data. When ever one changes the data, I need them to be reflected in the other instances instantly. I do not want to use static members, because the classes are not derived from a shared ancestor, also, I need to be able to break that reference if I want to, so it has it's own proper data set. Quote Link to comment https://forums.phpfreaks.com/topic/202808-unset-reference-on-php-class-member/#findComment-1063249 Share on other sites More sharing options...
Daniel0 Posted May 25, 2010 Share Posted May 25, 2010 Go look up PHP's object model. That's exactly how it works already without passing by reference. A little example: <?php class Test { public $foo; } function doSomething(Test $obj) { $obj->foo = 'hello'; } $foo = new Test(); $foo->foo = 'hi'; doSomething($foo); var_dump($foo->foo); Output: string(5) "hello" Quote Link to comment https://forums.phpfreaks.com/topic/202808-unset-reference-on-php-class-member/#findComment-1063264 Share on other sites More sharing options...
ignace Posted May 26, 2010 Share Posted May 26, 2010 When ever one changes the data, I need them to be reflected in the other instances instantly. This reminds me of the Observer pattern take a look at SplSubject and SplObserver Quote Link to comment https://forums.phpfreaks.com/topic/202808-unset-reference-on-php-class-member/#findComment-1063413 Share on other sites More sharing options...
phant0m Posted May 26, 2010 Author Share Posted May 26, 2010 Daniel0: the members have to be private. I'm already using some kind of observer pattern. I have built classes to work with hierarchical data. It can load the data from the database and store it back. I have 3 classes: a Manager, a Tree and a Node class. The manager holds all the data. You can request a (Sub)Tree from the manager. You then get an instance of the Tree class. It shares the same data as the Manager (passed by reference to the Tree when calling the constructor). When I want to look at a node from that Tree, I will request a node from the Tree, e.g. via getFirst() which will get the first subordinate. It also shares the same data. The node provides function to traverse the tree. getNext(), getFirstChild(), etc etc.... by default, these do not create a new node instance, but rather make the Node instance point to a different node in the tree data. The Node class basically knows the current node's parent id, and the index. When I want to getData() from the node, I basically return $this->nodes[$this->currentParentId][$currentIndex]; I can request another node from a Tree. Now, I'd have two instances of the Node class. They might point to the same node. If I change data on one, the changes are reflected immediately in the other node, because they work with the very same data. I do not even need to make use of the observer pattern. However, if I have 2 Node instances, and they both point to nodes with the same parent, the following situation might arise: 0 (parent) - 0 - 1 <- Node 1 - 2 - 3 <- Node 3 When I to remove the Node that Node 1 points to, Node 3 will be pointing to an invalid node. That's when I make use of the observer pattern, to notify all Nodes that there have been changes in the data structure. As information they will get the following: parentId where nodes have been changed, the position, and the amount (negative if removed, positive if inserted). Then, the nodes will adjust their indexes if necessary. Quote Link to comment https://forums.phpfreaks.com/topic/202808-unset-reference-on-php-class-member/#findComment-1063459 Share on other sites More sharing options...
phant0m Posted May 28, 2010 Author Share Posted May 28, 2010 Any suggestion? Or was my post completely incomprehensible? Just ask if you have a question regarding my data structure. I'll try to explain in more detail. Quote Link to comment https://forums.phpfreaks.com/topic/202808-unset-reference-on-php-class-member/#findComment-1064504 Share on other sites More sharing options...
phant0m Posted June 11, 2010 Author Share Posted June 11, 2010 ok... I'll give this another shot... Or I'll just leave it be the way it is now. thoughts anyone? Quote Link to comment https://forums.phpfreaks.com/topic/202808-unset-reference-on-php-class-member/#findComment-1070796 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.