我大大简化了我的代码,但我正在做的是这样的:
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()
只会被调用一次,但它不会执行实际的 closure
然后。
我还注意到,如果我在 __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
The constructor gets called:
public function __construct($name, $dependencies){ /* Code here */ } //Line: 05
2.1. Following parameters are passed:
$name
="App1"
$dependencies
=["PredefinedApp"]
You assign
$name
to$this->name
with this line:$this->name = $name; //Line: 06
You initialize
$apps
with an empty array:$apps = []; //Line: 08
Now you loop through each element of
$dependencies
, which has 1 element here (["PredefinedApp"]
)In the loop you do the following:
6.1 Assign the return value of a function call to an array index:
$apps[$name] = $dependName($this); //Line: 10 //$apps["App1"] = PredefinedApp($this);
You call the function:
PredefinedApp(){ /* Code here */} //Line: 28
Now you create again a new instance of:
App
inPredefinedApp()
same as before (Point 2 - 6, expect in the constructor you have other variable values + you don't enter the loop, since the array is empty)You assign a closure to a class property:
$app->cleanup = function($parent){ //Line: 31 //Do stuff like: echo "I'm saved"; };
You return the new created object of
App
:return $app; //Line: 34
Here already the
__destruct()
gets called, because when the function ends therefcount
goes to 0 of thatzval
and__destruct()
is triggered. But since$this->apps
is empty nothing happens here.The new created object in that function gets returned and assigned to the array index (Note: We are back from the function to point 6.1):
$apps[$name] = $dependName($this); //Line: 10 //$apps["App1"] = PredefinedApp($this);
The constructor ends with assigning the local array to the class property:
$this->apps = $apps; //Line: 12
Now the entire script ends (We have done line: 37)! Which means for the object
$app
the__destruct()
is triggered for the same reason as before for$app
in the functionPredefinedApp()
Which means you now loop through each element from
$this->apps
, which only holds the returned object of the function: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. So there is no
cleanup
class method, it's just a class property. Which then means__call()
gets invoked:public function __call($name, $arguments){ //Line: 21 if(is_callable([$this, $name])){ return call_user_func_array([$this, $name], $arguments); } }
The
$arguments
contains itself ($this
). Andis_callable([$this, $name])
isTRUE
, becausecleanup
is callable as closure.So now we are getting in the endless stuff, because:
return call_user_func_array([$this, $name], $arguments); //Line: 23
Is executed, which then looks something like this:
return call_user_func_array([$this, "cleanup"], $this);
Which then again tries to call
cleanup
as a method and again__call()
is invoked and so on...
So at the end of the entire script the hole disaster starts. But I have some good news, as complicated this sounds, the solution is much simpler!
Solution
Just change:
return call_user_func_array([$this, $name], $arguments); //Line: 23
with this:
return call_user_func_array($this->$name, $arguments);
//^^^^^^^^^^^ See here
因为像这样更改它,您不会尝试调用方法,而是调用闭包。所以如果你也把:
echo "I'm saved";
在闭包中分配它时,您将得到输出:
I'm saved
关于php - __destruct() 和 __call() 创建无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158883/