I simplified my code a lot, but what I am doing is something like this:
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;
}
I then create an app like this:
$app = new App('App1', ['PredefinedApp']);
It creates an App
instance, and then the items in the array create new app instances of whatever is defined and place them into an internal app array.
When I execute my destructor on the main app, it should call cleanup()
on all the child apps. But what is happening, is that it is executing an endless loop, and I am not sure why.
I do notice that if I comment out call_user_func_array
then __call()
is only called once, but it doesn't execute the actual closure
then.
I have also noticed, that if I do a var_dump()
in the __call()
it dumps endlessly. If I do a var_dump()
in cleanup()
instead I get a http 502
error.