Do parent class share memory with extended classes?

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

    Do parent class share memory with extended classes?

    Hello,

    I was wondering the cost of creating an instance of an extended class.

    By that I mean, if I have classes:
    - A
    - A1 and A2,
    where: A1 and A2 extend class A.

    what do instances A1 and A2 share of their common parent A in memory?

    a) Nothing
    b) Attributes
    c) just Methods
    d) Attributes and Methods

    by that I would like to know what has to be put in memory again?
    My guess is c) but I don't know for sure, and I think that I also depends on the lifetime of the two instances, if they overlap or not. If they don't everything has to be put in to memory again... doesn't it?

    And by the way, when I create two instances of the same class do they share the same amount of data than two subclasses with their parent (example above)?

    Thanks

    Regards,

    bilibytes
    Last edited by guillermobytes; Jan 24 '11, 12:17 AM. Reason: please edit title to : do same parent class share memory
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    I believe that is not the case. The parent class's memory is not shared between its children. Each one makes a copy for the object. Which makes sense because you do not want two objects to have identical data in them. I'm sure PHP has memory optimization methods and tricks.

    Consider the simple example below.

    Code:
    class Foo {
       public $bar;
    
    }
    
    class Baz1 extends Foo {
       public $mine; 
       function __constructor($val) { $this->bar = $val;}
    }
    
    class Baz2 extends Foo {
       public $mine; 
       function __constructor($val) { $this->bar = $val;}
    }
    
    $test1 = new Baz1('Test One');  //sets its $bar to 'Test One'
    $test2 = new Baz2('Test Two'); // sets its $bar to 'Test Two'
    
    echo $test1->bar; // outputs 'Test One'
    echo $test2->bar; // outputs 'Test Two'
    As you can see above, if the Foo class was in shared memory both would have output: 'Test Two' (The last value assigned to Foo->$bar). But that is not the case. Which means Foo->bar is in memory twice with two different values.

    Cheers,


    Dan

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      My guess at memory allocation:

      Assuming Foo class needs 64 Bytes and for some reason Baz1 and Baz2 class need 32 and 16 bytes, respectively. Then the following memory would be needed for $test1 and $test2 objects:

      $test1 = (64+32) = 98 Bytes
      $test2 = (64+16) = 80 Bytes

      If a PHP expert can comment, that would be fantastic!

      Dan

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Dan is right. Creating 2 objects of the same class leaves you with new distinct memory allocated for both of the objects.

        Comment

        • guillermobytes
          New Member
          • Jan 2010
          • 77

          #5
          thanks for replying dlite922,

          as you point out, it would not make sense to share attributes (class variables) between objects, because they need to be able to be different from each other.

          Static attributes are shared between all objects, that's why they are sometimes called class attributes, rather than instance/object attributes.

          But as you may have notice, methods do not change between objects. Let's say the have no state, I was wondering whether they were shared on every instantiation.
          You point out the memory cost, but there is also the allocation cpu instuctions cost.
          I explain what i mean : PHP is an interpreted language so every piece of code in our scripts is parsed on execution. So every time an instance of a class is created, the class definition must be read and stored in memory, and from that blueprint an instance is created in memory.
          That seems fair, if every new instance is of a different class type. But it would be a waste of cpu time to read that class again and again if it were already in memory.

          Ex class:
          Code:
          class A 
          {
            public $varA;
            public function methodA()
            {
              //20 lines of code
            }
          }
          
          class B
          {
            public $varB;
            public funciton methodB()
            {
              //20 lines of more code
            }
          }
          let's create some instances of these two classes :
          Code:
          $a = new A();
          $b = new B();
          $a2 = new A();
          So here it seems fair that the PHP parser put class A in memory, then create an instance of that class, by allocating memory for attribure $varA and methodA, and then associating the pointer of that instance to variable $a.
          Same thing on line 2 for var $b as class B is not already in memory...
          But now the important part, for $a2; class A was already put in memory so let's reuse that (skip parsing class A to put it in memory as we already did) and create an instance of class A. Now that's where my first post question was aimed at : when creating that new instance of class A do we have to allocate memory for methodA again, or there is a shared memory for methods?

          Well I can summarize the question : how does an object look like in memory?

          Comment

          Working...