By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,940 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,940 IT Pros & Developers. It's quick & easy.

Nested Object reference

P: 77

I'm creating a class, that clones itself whenever it gets a new value.

Each instance has a set of properties that are related to the value it gets:

Expand|Select|Wrap|Line Numbers
  1. class Atom
  2. {
  3.     private $_currentValue;
  4.     private $_origin;
  5.     private $_outdatedAtom;//reference to its old state
  7.     public function setValue($value, $origin)
  8.     {
  9.         if (null !== $this->_currentValue) {
  10.             $this->_outdatedAtom = clone $this;
  11.         }
  12.         $this->_origin = $origin;
  13.         $this->_currentValue = $value;
  14.     }
  16.     public function unsetOutdatedAtom()
  17.     {
  18.         //help here!
  19.     }
  20. }
whenever i pass a new value to setValue(), the instance gets cloned and i keep a reference of the old instance state.

So if y pass a new value to setValue() 10 times, i get a nested reference of 9 objects,

I want to create a method that would allow to cut the reference chain from the instance it is called in.

Expand|Select|Wrap|Line Numbers
  1. //unset the the $atom outdatedAtom's outdatedAtom reference
  2. $atom->getOutdatedAtom()->unsetOutdatedAtom();
  3. //this would leave us with just one reference, the current atom's outdatedAtom, all references down the chain are lost
What i need to know for the implementation of unsetOutdatedAtom() (aka cutOutdatedAtomChain) is: do i need to go all down the chain of references and call the unsetOutdatedAtom() method from the last atom that has an outdatedAtom reference, or just unsetting the current outdatedAtom's reference will be enough.

I ask this because i dont want to have a big chain of outdated atoms that get dereferenced and occupy memory space.

I dont know if i made myself clear.
Apr 13 '10 #1
Share this Question
Share on Google+
10 Replies

P: 77
Ok i will abstract my question:

whenever an object is the only one referencing a set of other objects.
if that object gets unset, will the objects that it references get cleaned from memory?
Apr 13 '10 #2

Expert Mod 5K+
P: 8,639
out of curiousity, why do you need nested objects?
Apr 13 '10 #3

P: 77
well maybe I didn't express it well, by nested object reference i mean that an object has a property which is a pointer to another object, which in turn has a property that is a pointer to another etc.

I need that because every parent has a child and every child is a parent

I don't know how to explain it better.

btw do you know if i kill the parent object, will its childs get removed from memory?
Apr 13 '10 #4

Expert Mod 5K+
P: 8,639
I did understand that part, but not why you would need that.

I guess, but Im not familiar with PHPs memory internals
Apr 13 '10 #5

P: 77
because I can't figure out a way to represent that parent->child relationship other than with nested reference. I can use an array but as I like to program OO I need nested reference.
Apr 13 '10 #6

Expert Mod 5K+
P: 8,639
I’m sure there is a better solution for your problem. the nested objects just don’t feel right.
Apr 14 '10 #7

P: 77
This works fine, nested reference is used everywhere in OOP. But if you see another approach i would be glad to have a look at it
Apr 14 '10 #8

Expert Mod 5K+
P: 8,639
yea, one or two levels … that’s still manageable. and most often those child classes are of a different class.

I would have to know what you want to do, to give you a sensible answer.
Apr 14 '10 #9

Expert 5K+
P: 6,050
Memory is only released once there are no references to a variable/object/resource left.
Apr 14 '10 #10

P: 77
ok thanks Markus
Apr 14 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.