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” >您使用以下行将$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点,在构造函数中期望您有其他变量值不进入循环,因为数组为空) $app->cleanup = function($parent){ //Line: 31 //Do stuff like: echo "I'm saved"; }; >您返回App的新创建对象: return $app; //Line: 34 >这里已经是 $apps[$name] = $dependName($this); //Line: 10 //$apps["App1"] = PredefinedApp($this); >构造函数以将本地数组分配给class属性结束: $this->apps = $apps; //Line: 12 >现在整个脚本结束了(我们已完成第37行)!这意味着对象$app 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.所以没有清理类方法,它只是一个类属性.这意味着 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()等等…… 因此,在整个脚本结束时,洞穴灾难开始了.但是我有一些好消息,听起来很复杂,解决方案要简单得多! 解 只是改变: 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |