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.