2

In PHP, if you make an array of objects, are the object methods (not data members) copied for each instance of the object in the array, or only once? I would assume that for memory reasons, the latter is true; I just wanted to confirm with the StackOverflow community that this is true.

For example, suppose I have a class MyClass with a couple of methods, i.e.

class MyClass {
    public $data1;
    private $data2;
    public function MyClass($d1, $d2) { 
        $this->data1=$d1;   $this->data2=$d2;
    }
    public function method1() {  }
    public function method2() {  }
}

Obviously in reality method1() and method2() are not empty functions. Now suppose I create an array of these objects:

$arr = array();
$arr[0] = & new MyClass(1,2);
$arr[1] = & new MyClass(3,4);
$arr[2] = & new MyClass(5,6);

Thus PHP is storing three sets of data members in memory, for each of the three object instances. My question is, does PHP also store copies of method1() and method2() (and the constructor), 3 times, for each of the 3 elements of $arr? I'm trying to decide whether an array of ~200 objects would be too memory-intensive, because of having to store 200 copies of each method in memory.

Thanks for your time.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
user1004061
  • 103
  • 1
  • 9
  • 3
    Surely a method is, by definition, a *reference* to a procedure? In which case, the mechanics of the procedure would be defined only once (when you load the class, *not* when you instantiate an object) and all instances would simply point to that procedure. This is pure speculation but I can't see it being any other way for any type of method/function, unless it is a closure. – DaveRandom Oct 19 '11 at 21:44
  • Hmm. good question. Wonder what'd happen to any static vars defined inside those methods. Would it be one static var shared by every instance of the method, or would each instance gets its own unique static var. – Marc B Oct 19 '11 at 22:03
  • @DaveRandom In Javascript, when you define a class, the methods are embedded in the object. Their content are not "shared" between the instances. – Matthieu Napoli Oct 19 '11 at 22:08
  • @Matthieu Agreed, but Javascript is a completely different ball game since more or less *eveything* in JS is in itself an object - including the method. But equally it depends how you define the method - if you do `String.prototype.methodName = function () {};` every string object has a method called `methodName` but they don't all have their own copy of the procedure, they will call the method that belongs to `String.prototype`. Until you do `mystr.methodName = function () {};` and override that method, then `mystr` has it's own copy. But for PHP (I believe) my statement is correct. – DaveRandom Oct 20 '11 at 21:21

2 Answers2

4

By definition (and that is your code), a function only exists once. That's why you produce code (and not data).

However, you can then use your code to produce a lot of data. But that's another story ;).

So unless you do not needlessly duplicate code across objects, your function(s) will only exist once. Independent how many instances of the code you create. Only the data associated to the code (the class members) are duplicated.

Sounds fair?

BTW:

$arr[0] = & new MyClass(1,2);

Gives you a strict standards error. You can not assign a reference/alias with the new keyword. Probably this way of writing is influenced by PHP 4 code, but this has changed since PHP 5 which introduced the object store.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • "By definition, a function only exists once" > No, that is not the case in Javascript for example, see my answer. However, it is true in PHP. – Matthieu Napoli Oct 19 '11 at 22:15
  • Well you should read my comment then... I said "for example". It's just that you are saying a wrong statement, a function doesn't alway exist once. I think the OP asked an interesting question, given that is not *always* the same answer depending on the language, so that is worth being "picky" and precise. (and rage-downvote if you like, that is very mature) – Matthieu Napoli Oct 19 '11 at 22:25
  • Thanks for the heads up about the strict standards error. I am fairly new to PHP5 programming and I appreciate the tip. – user1004061 Oct 19 '11 at 22:30
  • From the OP: Well the consensus seems to be that functions are only defined once, regardless of the number of instances of each object. This is exactly what I would expect. Thanks everyone for their assistance. BTW, it's been a while since I've posted to the Stackoverflow forums. If there is a way to mark thread resolved, please let me know. Thanks! – user1004061 Oct 19 '11 at 22:42
0

The method content will be stored only once in memory. Every PHP object (of the said class) will have a reference to the method.

So to conclude: you don't have to care about the memory size of your methods if you plan to have a lot of objects. Just care about the memory size of the objects attributes.

This is, for example, different than in Javascript, where each class-defined method is contained in each instance. However, if you define a method in its prototype, then the method is shared by all class instances (lighter in memory of course). See this link: http://webdevelopersjournal.com/articles/jsintro3/js_begin3.html.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • What you say about javascript is wrong. This does not work as you describe it for all javascript interpreters, you should actually name the correct language name and version where this is the case. – hakre Oct 19 '11 at 22:23
  • @hakre Would you care being more precise ?? – Matthieu Napoli Oct 19 '11 at 22:26