call_user_func_array 非静态方法调用
call_user_func_array non static method call
我有这个 class 用于测试建议,它是使用 FastRoute
在加载库的同一页面上加载的:
class Controller {
public function demo()
{
echo 'Hello world';
}
}
每次我访问项目的根目录时,我都会收到这个奇怪的错误
Deprecated: Non-static method Controller::demo() should not be called statically
我不明白为什么 call:user_func_array()
认为该方法是静态的。
这里是路由的测试代码。任何建议表示赞赏
<?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;
}
?>
是因为您没有指定 class 实例(对象),而是 class 本身。
尝试用 new $class()
.
实例化一个新对象
我有这个 class 用于测试建议,它是使用 FastRoute
在加载库的同一页面上加载的:
class Controller {
public function demo()
{
echo 'Hello world';
}
}
每次我访问项目的根目录时,我都会收到这个奇怪的错误
Deprecated: Non-static method Controller::demo() should not be called statically
我不明白为什么 call:user_func_array()
认为该方法是静态的。
这里是路由的测试代码。任何建议表示赞赏
<?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;
}
?>
是因为您没有指定 class 实例(对象),而是 class 本身。
尝试用 new $class()
.