I am getting the following error when using the code below the error.
can you tell me what i am doing wrong?
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in D:\dev\TESTCASE_classes_layout\main.class.php on line 18
Here is the code I use :
<?php
include('test_class1.php');
include('test_class2.php');
include('test_class3.php');
class Main_Class{
protected $test_class1;
protected $test_class2;
protected $test_class3;
private $objects_array = array();
public function Main_Class(){
$this->test_class1 = new Test_Class1();
$this->objects_array['test_class1'] = $this->test_class1;
$this->test_class2 = new Test_Class2();
$this->objects_array['test_class2'] = $this->test_class2;
$this->test_class3 = new Test_Class3();
$this->objects_array['test_class3'] = $this->test_class3;
}
public function get_Objects(){
return $this->objects_array;
}
}
?>
Here is the code I use for all three test classes. It is exactly the same code only for class name number and the function name number.
<?php
class Test_Class1 extends Main_Class{
function test1(){
return 'hello';
}
}
?>
It has to do with the extending part. Cause when I delete the extending part it works.
Here is my goal:
I am trying to instantiate the classes in this class and extend from it so all classes can call each other without generating another instance of the class.
maybe there is a better way of doing that so if you know that then let me know.
A constructor is called everytime you create an object of type
Main_Class, or one of its subclasses.Since you’re creating new objects of a subtype during this creation, you run into an infinite loop that ends when memory is exhausted or the memory limit is reached:
Instead, you should create the objects in a static method. But even if you do, note that your goal violates the important principle of encapsulation and will lead to an extremely poor design. Instead, you probably want to have methods that operate on an arbitrary object of type
Main_Class, and override the methods (i.e. have a methodtestin every one of these classes).