【发布时间】:2015-11-16 11:51:51
【问题描述】:
我大大简化了我的代码,但我正在做的是这样的:
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 错误。
【问题讨论】:
-
@RyanVincent 如果您将所有代码放入一个文件中,然后运行该文件,您应该会看到问题
-
我很确定这是由于您重载
__call()然后使用call_user_func_array()... 再次调用__call()造成的。永远。或者至少在你内存不足之前。 -
我正在 atm 调试它,我认为 __desctruct() 导致了这一切。因为如果我删除它一切正常
标签: php oop methods closures destructor