Nested Object reference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guillermobytes
    New Member
    • Jan 2010
    • 77

    Nested Object reference

    Hi,

    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:

    Code:
    class Atom
    {
    	private $_currentValue;
    	private $_origin;
    	private $_outdatedAtom;//reference to its old state
    
    	public function setValue($value, $origin)
    	{
    		if (null !== $this->_currentValue) {
    			$this->_outdatedAtom = clone $this;
    		}
    		$this->_origin = $origin;
    		$this->_currentValue = $value;
    	}
    
    	public function unsetOutdatedAtom()
    	{
    		//help here!
    	}
    }
    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.

    Code:
    //unset the the $atom outdatedAtom's outdatedAtom reference
    $atom->getOutdatedAtom()->unsetOutdatedAtom();
    //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 unsetOutdatedAt om() (aka cutOutdatedAtom Chain) is: do i need to go all down the chain of references and call the unsetOutdatedAt om() 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.
  • guillermobytes
    New Member
    • Jan 2010
    • 77

    #2
    Originally posted by guillermobytes
    Hi,

    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:

    Code:
    class Atom
    {
    	private $_currentValue;
    	private $_origin;
    	private $_outdatedAtom;//reference to its old state
    
    	public function setValue($value, $origin)
    	{
    		if (null !== $this->_currentValue) {
    			$this->_outdatedAtom = clone $this;
    		}
    		$this->_origin = $origin;
    		$this->_currentValue = $value;
    	}
    
    	public function unsetOutdatedAtom()
    	{
    		//help here!
    	}
    }
    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.

    Code:
    //unset the the $atom outdatedAtom's outdatedAtom reference
    $atom->getOutdatedAtom()->unsetOutdatedAtom();
    //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 unsetOutdatedAt om() (aka cutOutdatedAtom Chain) is: do i need to go all down the chain of references and call the unsetOutdatedAt om() 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.
    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?

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      out of curiousity, why do you need nested objects?

      Comment

      • guillermobytes
        New Member
        • Jan 2010
        • 77

        #4
        Originally posted by Dormilich
        out of curiousity, why do you need nested objects?
        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?

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by guillermobytes
          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 did understand that part, but not why you would need that.


          Originally posted by guillermobytes
          btw do you know if i kill the parent object, will its childs get removed from memory?
          I guess, but I’m not familiar with PHP’s memory internals

          Comment

          • guillermobytes
            New Member
            • Jan 2010
            • 77

            #6
            Originally posted by Dormilich
            I did understand that part, but not why you would need that.



            I guess, but I’m not familiar with PHP’s memory internals
            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.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              I’m sure there is a better solution for your problem. the nested objects just don’t feel right.

              Comment

              • guillermobytes
                New Member
                • Jan 2010
                • 77

                #8
                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

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  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.

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Memory is only released once there are no references to a variable/object/resource left.

                    Comment

                    • guillermobytes
                      New Member
                      • Jan 2010
                      • 77

                      #11
                      ok thanks Markus

                      Comment

                      Working...