从不同的类调用一个方法两次会导致内存不足错误


calling a method twice from different classes causes an out of memory error

>我在单例类中有一个方法,这个类是从子类外出的父类调用的。

假设我的类已正确声明和创建,这只是我的编码逻辑的快速运行。

class Singleton
{
    public function load_sys()
    {
        $this->something();
    }
    public function something()
    {
        $this->load();
    }
    public function load(){}
}
class Parent
{
    public function __construct()
    {
        $this->sys = Singleton::init();
        $this->sys->load_sys();
    }
}
class Child extends Parent
{
    public function __construct()
    {
        parent::__construct();
    }
}

当我这样做时,我收到此错误:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 4296 bytes) in ~~~.php on line 102

可能是什么原因造成的?

我认为

这可能不是由于两次调用方法,而是由于代码中的一些错误。首先,你声明一个带括号的类:

class Singleton()

应该是:

class Singleton

此外,您正在将一个类声明为 Parent 这是 PHP 中的保留字,因此请先尝试重命名它。