Jump to content

Unset reference on PHP Class member


phant0m

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.