I want to make var1 the array of instances of Class1. This array must be defined in Class2. How to do this correctly?
class Class1 {
public $values = array();
}
class Class2 {
public $var1 = array();
public $var1_ptr;
public function setPtr(Class1 $var1) {
$this->var1_ptr = $var1;
}
}
$cl2 = new Class2();
$cl2->sePtr($cl2->$var1[0]); // Here I want to access the first array's object of Class1.
You can store objects as array elements, and access them no differently as if they were their own variables. The same applies to storing them as a property of an object.
To retrieve the $j-th value of the $i-th object stored in the array: