微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – __destruct()和__call()创建无限循环

我简化了我的代码,但我正在做的是这样的:

class App{

    protected $apps = [];

    public function __construct($name, $dependencies){
        $this->name = $name;

        $apps = [];
        foreach($dependencies as $dependName){
            $apps[$name] = $dependName($this); // returns an instance of App
        }
        $this->apps = $apps;
    }

    public function __destruct(){
        foreach($this->apps as $dep){
            $result = $dep->cleanup($this);
        }
    }

    public function __call($name, $arguments){
        if(is_callable([$this, $name])){
            return call_user_func_array([$this, $name], $arguments);
        }
    }
}


function PredefinedApp(){
    $app = new App('PredefinedApp', []);

    $app->cleanup = function($parent){
        // Do some stuff
    };
    return $app;
}

然后我创建一个这样的应用程序:

$app = new App('App1', ['PredefinedApp']);

它创建一个App实例,然后数组中的项创建任何已定义的新应用实例,并将它们放入内部应用程序数组中.

当我在主应用程序上执行析构函数时,它应该在所有子应用程序上调用cleanup().但正在发生的事情是,它正在执行无限循环,我不知道为什么.

我注意到如果我注释掉call_user_func_array,那么__call()只被调用一次,但它不会执行实际的闭包.

我也注意到,如果我在__call()中执行var_dump(),它会无休止地转储.如果我在cleanup()中执行var_dump()而得到http 502错误.

解决方法:

因此,让我们浏览代码,看看这里发生了什么以及为什么:

01|    class App{ 
02|
03|        protected $apps = [];
04|
05|        public function __construct($name, $dependencies){
06|            $this->name = $name;
07|
08|            $apps = [];
09|            foreach($dependencies as $dependName){
10|                $apps[$name] = $dependName($this);
11|            }
12|            $this->apps = $apps;
13|        }
14|
15|        public function __destruct(){
16|            foreach($this->apps as $dep){
17|                $result = $dep->cleanup($this);
18|            }
19|        }
20|
21|        public function __call($name, $arguments){
22|            if(is_callable([$this, $name])){
23|                return call_user_func_array([$this, $name], $arguments);
24|            }
25|        }
26|    }
27|	
28|    function PredefinedApp(){
29|        $app = new App('PredefinedApp', []);
30|
31|        $app->cleanup = function($parent){
32|            //Do stuff like: echo "I'm saved";
33|        };
34|        return $app;
35|    }
36|		
37|    $app = new App('App1', ['PredefinedApp']);

注意:为代码的每一行添加行号,因此我可以在下面的答案中引用这些行

问题

>您使用以下行创建:App的实例:

$app = new App('App1', ['PredefinedApp']);  //Line: 37

>构造函数调用

public function __construct($name, $dependencies){ /* Code here */ }  //Line: 05

2.1.以下参数传递:

> $name =“App1”
> $dependencies = [“PredefinedApp”]

>您使用以下行将$name分配给$this-> name:

$this->name = $name;  //Line: 06

>使用空数组初始化$apps:

$apps = [];  //Line: 08

>现在循环遍历$dependencies的每个元素,这里有1个元素([“PredefinedApp”])
>在循环中,您执行以下操作:

6.1将函数调用的返回值赋给数组索引:

$apps[$name] = $dependName($this);  //Line: 10
//$apps["App1"] = PredefinedApp($this);

>你调用这个函数

PredefinedApp(){ /* Code here */}  //Line: 28

>现在再次创建一个新实例:PredefinedApp()中的App与之前相同(第2 – 6点,在构造函数中期望您有其他变量值不进入循环,因为数组为空)
>您将closure分配给类属性

$app->cleanup = function($parent){  //Line: 31
    //Do stuff like: echo "I'm saved";
};

>您返回App的新创建对象:

return $app;  //Line: 34

>这里已经是__destruct() gets called了,因为当函数结束时,refcount变为该zval的0并触发__destruct().但由于$this->应用程序是空的,因此这里没有任何事情发生.
>返回该函数中新创建的对象并将其分配给数组索引(注意:我们从函数返回到6.1):

$apps[$name] = $dependName($this);  //Line: 10
//$apps["App1"] = PredefinedApp($this);

>构造函数以将本地数组分配给class属性结束:

$this->apps = $apps;  //Line: 12

>现在整个脚本结束了(我们已完成第37行)!这意味着对象$app __destruct() is triggered的原因与以前在函数PredefinedApp()中的$app相同
>这意味着你现在循环遍历来自$this-> apps的每个元素,它们只保存函数的返回对象:

public function __destruct(){  //Line: 15
    foreach($this->apps as $dep){
        $result = $dep->cleanup($this);
    }
}
Array(
    "App1" => App Object
        (
            [apps:protected] => Array
                (
                )

            [name] => PredefinedApp
            [cleanup] => Closure Object
                (
                    [parameter] => Array
                        (
                            [$parent] => <required>
                        )

                )

        )
)

>对于每个元素(这里只有1个),您执行:

$result = $dep->cleanup($this);  //Line: 17

But you don’t call the closure! It tries to call a class method.所以没有清理类方法,它只是一个属性.这意味着__call() gets invoked

public function __call($name, $arguments){  //Line: 21
    if(is_callable([$this, $name])){
        return call_user_func_array([$this, $name], $arguments);
    }
}

> $arguments包含它自己($this).并且is_callable([$this,$name])为TRUE,因为cleanup可以作为闭包调用.
>所以现在我们进入了无穷无尽的东西,因为:

return call_user_func_array([$this, $name], $arguments);  //Line: 23

执行,然后看起来像这样:

return call_user_func_array([$this, "cleanup"], $this);

然后再次尝试将清理作为方法调用,再次调用__call()等等……

因此,在整个脚本结束时,洞穴灾难开始了.但是我有一些好消息,听起来很复杂,解决方案要简单得多!

只是改变:

return call_user_func_array([$this, $name], $arguments);  //Line: 23

有了这个:

return call_user_func_array($this->$name, $arguments);
                           //^^^^^^^^^^^ See here

因为通过这样更改它你不会尝试调用方法,而是关闭.所以如果你也把:

echo "I'm saved";

在分配时,在结束时,您将得到输出结果:

I'm saved

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐