脚本宝典收集整理的这篇文章主要介绍了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,$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”])
>在循环中,您执行以下操作:
$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);
$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,$arguments); } }
> $arguments包含它自己($this).并且is_callable([$this,$name])为TRUE,因为cleanup可以作为闭包调用.
>所以现在我们进入了无穷无尽的东西,因为:
return call_user_func_array([$this,$arguments); //Line: 23
执行,然后看起来像这样:
return call_user_func_array([$this,"cleanup"],$this);
然后再次尝试将清理作为方法调用,再次调用__call()等等……
因此,在整个脚本结束时,洞穴灾难开始了.但是@L_304_56@一些好消息,听起来很复杂,解决方案要简单得多!
解
只是改变:
return call_user_func_array([$this,$arguments); //Line: 23
有了这个:
return call_user_func_array($this->$name,$arguments); //^^^^^^^^^^^ See here
因为通过这样更改它你不会尝试调用方法,而是关闭.所以如果你也把:
echo "I'm saved";
在分配时,在结束时,您将得到输出结果:
I'm saved
以上是脚本宝典为你收集整理的php – __destruct()和__call()创建无限循环全部内容,希望文章能够帮你解决php – __destruct()和__call()创建无限循环所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。