1

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;
}

?>
3
  • 1
    call_user_func_array([new $class(), $method], $vars); Commented Jul 23, 2019 at 14:41
  • 1
    *"I can't understand why the call:user_func_array() is thinking that the method is static." -- it doesn't. On the contrary, it says that the method is not static and you try to call it in a static manner. I.e. without using an instance but the class name. Commented Jul 23, 2019 at 14:58
  • @axiac it was a mistake I've made, I forgot to put the new keyword before the $class variable. This because I was using PHP-DI for dependency injection, but not in this case. Commented Jul 23, 2019 at 15:00

1 Answer 1

1

Is because you're not specifying a class instance (object) but the class itself.
Try to instantiate a new object with new $class().

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help. It was a fault of mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.