I have faced a curious condition and maybe you can help me understand that.
$object = array('controller' => 'frontend_shop', 'method' => 'category');
include_once(PATH.'controllers/'.$object['controller'].'.php');
$controller = new $object['controller']($object);
class Frontend_shop extends Controller {
public $controller;
public function __construct($object)
{
// Works
$this->$object['method']();
//Don´t work
$this->controller = $object;
$this->controller['method']();
}
public function category()
{
echo 'hello';
}
}
This works and ‘hello’ is displayed
$this->$object['method']();
But when I assign this array to a class variable like:
$this->controller = $object;
$this->controller['method']();
I get:
Fatal error: Call to undefined function category() in /usr/lib/app/application/controllers/frontend_shop.php on line 10
Of course I know that I can use the first method, but maybe you can explain whats could be wrong in the class variable way of do it. Thanks
you would need something like
$this->controller['method']resolves to a string(the method name). but methods must be called upon an object(otherwise they’re functions, not methods), and thats where the other$this->resolution comes in.I prefer
I don’t like the litteral call syntax, as you have to look closely at the code to see whats really going on.
http://php.net/manual/en/function.call-user-func.php
http://www.php.net/manual/en/language.types.callable.php