I have this class for test propose that is loaded using FastRoute on the same page where the library is loaded:
class Controller {
public function demo()
{
echo 'Hello world';
}
}
Every time I access the root of my project which is a mapped route, I get this strange error
Deprecated: Non-static method Controller::demo() should not be called statically
I can't understand why the call:user_func_array() is thinking that the method is static.
here is the test code for routing. Any suggestion is appreciated
<?php
require_once __DIR__.'/vendor/autoload.php';
use FastRoute\RouteCollector;
use FastRoute\simpleDispatcher;
use FastRoute\Dispatcher;
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r){
// test route
$r->addRoute('GET', '/', 'Controller@demo');
});
// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];
// Strip query string (?foo=bar) and decode URI
if (false !== $pos = strpos($uri, '?')) {
$uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);
$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
// ... 404 Not Found
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$allowedMethods = $routeInfo[1];
// ... 405 Method Not Allowed
break;
case FastRoute\Dispatcher::FOUND:
$handler = $routeInfo[1];
$vars = $routeInfo[2];
list($class, $method) = explode("@", $handler, 2);
call_user_func_array([$class, $method], $vars);
break;
}
?>
call_user_func_array([new $class(), $method], $vars);