ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How PHP deals with OOP in memory

June 14, 2010 6:33pm

Subscribe [1]
  • #1 / Jun 14, 2010 6:33pm

    glopv2

    8 posts

    I know PHP will only copy data if necessary. So, in this code:

    $a = 'HELLO WORLD';
    $b = $a;
    $b = $b . '!';

    PHP never took the time to copy the ‘HELLO WORLD’ string until line 3. However, what about multiple instances of the same class? If I have:

    class hworld {
      var $c;
      public function meth() {
        $c = 'HELLO WORLD!';
      }
    }

    and later I run:

    $foo = new hworld();
    $bar = new hworld();
    $bar->meth();

    Now I clearly have two values of $c. My question is: PHP clearly has to make a copy of the object—but does it also make a (seemingly useless) copy of the meth() method?

    I’m only curious for the purpose of writing good code. I hope it doesn’t, but if it does I’d like to know to avoid many copies of classes with large methods.

    Thoughts? Am I misunderestimating PHP?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases