php - Mvc Method name must be a string -
i have problem , dont have idea how slove this.
problem on line code:
public function executeaction() { return $this->{$this->action}(); }
all other work fine controller success loaded have fatal error this.
fatal error: method name must string in d:\xampp\htdocs\workplace\mvc\lib\basecontroller.php on line 27
check code:
index.php
$fcontroller = new fcontroller($_get); $controller = $fcontroller->createcontroller(); $controller->executeaction();
fcontorler
public function createcontroller() { if(class_exists($this->controller)) { $parent = class_parents($this->controller); if(in_array('basecontroller', $parent)) { if(method_exists($this->controller, $this->action)) { return new $this->controller($this->action, $this->url); }else { echo "method no exists"; } }else { echo "bad controller"; } } else { echo "controller ". $this->controller . " class no exists"; } }
basecontroller
abstract class basecontroller { protected $urlvalues; protected $action; /* * construct * * @param string $action * @param array $url * */ public function __construct($action, $urlvalues) { $this->action = $action; $this->urlvalues = $urlvalues; } /* * execute acction * */ public function executeaction() { return $this->{$this->action}(); }
localhost/workplace/mvc/index.php?controller=hello&action=say&id=5
var_dump($controller); object(hello)#2 (2) { ["urlvalues":protected]=> null ["action":protected]=> null }
the error triggered because trying use null
while interpreter expecting string.
as var_dump
shows, $this->action
null
therefore string interpolation $this->{$this->action}();
translated $this->{null}();
cannot called.
Comments
Post a Comment