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
致命错误:第18行的D:\ dev \ TESTCASE_classes_layout \ main.class.php中允许的内存大小为134217728个字节(试图分配32个字节)
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.
也许有更好的方法,所以如果你知道,那么让我知道。
4 个解决方案
#1
1
A constructor is called everytime you create an object of type Main_Class
, or one of its subclasses.
每次创建Main_Class类型的对象或其子类之一时,都会调用构造函数。
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:
由于您在创建过程中创建了子类型的新对象,因此会遇到无限循环,该循环在内存耗尽或达到内存限制时结束:
new Main_Class() calls ...
Constructor of Main_Class calls ...
new Test_Class1() calls ...
Constructor of Test_Class1 calls ...
Constructor of Main_Class calls ...
new Test_Class1() calls ...
...
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 method test
in every one of these classes).
相反,您应该在静态方法中创建对象。但即使你这样做,也要注意你的目标违反了封装的重要原则,并且会导致设计极其糟糕。相反,您可能希望拥有对Main_Class类型的任意对象进行操作的方法,并覆盖这些方法(即在每个类中都进行方法测试)。
#2
1
Dude this memory exhausted error is occurring because all three classes are extending the main class and in the constructor of the mail class you are calling the all 3 classes..
老兄这个内存耗尽错误正在发生,因为所有三个类都在扩展主类,而在邮件类的构造函数中,你正在调用所有3个类。
Which means when you will try to create the instance of any of the child classes they will go to parent class which will in turn call them again..Hence you are in a infinite loop..and php memory is exhausted..
这意味着当你尝试创建任何子类的实例时,它们将转到父类,而父类又会再次调用它们。因为你处于一个无限循环中......并且php内存已经耗尽。
That's why it is giving error if you are trying to to extending..and working preety well without that..
这就是为什么如果你试图扩展它并且在没有那个的情况下工作得很好的话就会给出错误的原因。
now the next part is how to get attributes of other 2 classes..
现在接下来的部分是如何获取其他2个类的属性..
First of all I don't see any reason you need such case, you can just define all the functions in a class and extend it in all and call them... If you still persist you have no other choice but creating instance seperately...:)
首先,我没有看到你需要这种情况的任何理由,你可以只定义一个类中的所有函数并将其扩展并调用它们......如果你仍然坚持,除了单独创建实例之外别无选择。 .. :)
#3
1
You're getting infinite recursion. Try adding a constructor function in Test_Class1
such as
你得到了无限的递归。尝试在Test_Class1中添加构造函数,例如
function Test_Class1 () {}
to prevent the call to the base constructor when instantiating Test_Class1
在实例化Test_Class1时阻止对基础构造函数的调用
EDIT: As a general note, you should never instantiate the same class or a child class in a class constructor as this leads to your issue, infinite recursion. Luckily for you, PHP does not automatically call the parent's constructor when inheriting and specifying a child constructor. However, in most other OO languages, this pattern would break 100% of the time.
编辑:作为一般说明,你不应该在类构造函数中实例化同一个类或子类,因为这会导致你的问题,无限递归。幸运的是,PHP在继承和指定子构造函数时不会自动调用父的构造函数。但是,在大多数其他OO语言中,此模式将在100%的时间内中断。
#4
0
Check this thread Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php as it may help you out. It actually might not relate with your script but with your configuration.
检查此线程允许内存大小33554432字节耗尽(试图分配43148176字节)在PHP中,因为它可能会帮助你。它实际上可能与您的脚本无关,但与您的配置有关。
Also check instantiation as it might be tricky to read the memory use given by the error. Maybe there's a loop of some kind. Are you using some framework with plugins or so?
还要检查实例化,因为读取错误给出的内存使用可能很棘手。也许有某种循环。您是否正在使用某些带插件的框架?
#1
1
A constructor is called everytime you create an object of type Main_Class
, or one of its subclasses.
每次创建Main_Class类型的对象或其子类之一时,都会调用构造函数。
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:
由于您在创建过程中创建了子类型的新对象,因此会遇到无限循环,该循环在内存耗尽或达到内存限制时结束:
new Main_Class() calls ...
Constructor of Main_Class calls ...
new Test_Class1() calls ...
Constructor of Test_Class1 calls ...
Constructor of Main_Class calls ...
new Test_Class1() calls ...
...
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 method test
in every one of these classes).
相反,您应该在静态方法中创建对象。但即使你这样做,也要注意你的目标违反了封装的重要原则,并且会导致设计极其糟糕。相反,您可能希望拥有对Main_Class类型的任意对象进行操作的方法,并覆盖这些方法(即在每个类中都进行方法测试)。
#2
1
Dude this memory exhausted error is occurring because all three classes are extending the main class and in the constructor of the mail class you are calling the all 3 classes..
老兄这个内存耗尽错误正在发生,因为所有三个类都在扩展主类,而在邮件类的构造函数中,你正在调用所有3个类。
Which means when you will try to create the instance of any of the child classes they will go to parent class which will in turn call them again..Hence you are in a infinite loop..and php memory is exhausted..
这意味着当你尝试创建任何子类的实例时,它们将转到父类,而父类又会再次调用它们。因为你处于一个无限循环中......并且php内存已经耗尽。
That's why it is giving error if you are trying to to extending..and working preety well without that..
这就是为什么如果你试图扩展它并且在没有那个的情况下工作得很好的话就会给出错误的原因。
now the next part is how to get attributes of other 2 classes..
现在接下来的部分是如何获取其他2个类的属性..
First of all I don't see any reason you need such case, you can just define all the functions in a class and extend it in all and call them... If you still persist you have no other choice but creating instance seperately...:)
首先,我没有看到你需要这种情况的任何理由,你可以只定义一个类中的所有函数并将其扩展并调用它们......如果你仍然坚持,除了单独创建实例之外别无选择。 .. :)
#3
1
You're getting infinite recursion. Try adding a constructor function in Test_Class1
such as
你得到了无限的递归。尝试在Test_Class1中添加构造函数,例如
function Test_Class1 () {}
to prevent the call to the base constructor when instantiating Test_Class1
在实例化Test_Class1时阻止对基础构造函数的调用
EDIT: As a general note, you should never instantiate the same class or a child class in a class constructor as this leads to your issue, infinite recursion. Luckily for you, PHP does not automatically call the parent's constructor when inheriting and specifying a child constructor. However, in most other OO languages, this pattern would break 100% of the time.
编辑:作为一般说明,你不应该在类构造函数中实例化同一个类或子类,因为这会导致你的问题,无限递归。幸运的是,PHP在继承和指定子构造函数时不会自动调用父的构造函数。但是,在大多数其他OO语言中,此模式将在100%的时间内中断。
#4
0
Check this thread Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php as it may help you out. It actually might not relate with your script but with your configuration.
检查此线程允许内存大小33554432字节耗尽(试图分配43148176字节)在PHP中,因为它可能会帮助你。它实际上可能与您的脚本无关,但与您的配置有关。
Also check instantiation as it might be tricky to read the memory use given by the error. Maybe there's a loop of some kind. Are you using some framework with plugins or so?
还要检查实例化,因为读取错误给出的内存使用可能很棘手。也许有某种循环。您是否正在使用某些带插件的框架?