Undefined Property ИмяКонтроллера::$имяМетода
Пробую писать свою MVC-шку. При попытке сделать вызов метода по такому принципу
$controllerObject->$methodName выдаёт ошибку
Notice: Undefined property: NewsController::$index in C:\Users\admin\PhpstormProjects\jolyMVC\App\Components\Router.php on line 42
КОД
$uri = $this->getURI();
foreach ($this->routes as $uriPattern => $path) {
if (preg_match("~$uriPattern~", $uri)) {
$segments = explode('@', $path);
$controllerName = array_shift($segments);
$methodName = array_shift($segments);
echo $methodName;
$controllerFile = ROOT."/controllers/".$controllerName.".php";
if (file_exists($controllerFile)) {
require_once($controllerFile);
}
$controllerObject = new $controllerName;
$result = $controllerObject->$methodName;
if ($result != null) {
break;
}
}
}
Как мне убрать $ у метода?
Ответы (1 шт):
Автор решения: Alexander Chernykh
→ Ссылка
$methodName нужно было заменить на $methodName()
Спасибо пользователю @u_mulder за быстрый и верный ответ!