Skip to content

Error when trying to use an object & method as a handler #25

Description

@mav2287

I am trying to register a route with fast route using an Object and static method with the following code:

static public function registerRoute( RouteCollector &$r ): void {
  $r->addRoute(['GET', 'OPTIONS'], '/password', array( self::class, 'renderChallenge') );
}

instead of the handler being called I instead get the following error message:

Argument 1 passed to Middlewares\Utils\CallableHandler::__construct() must be callable, object given, called in /src/vendor/middlewares/utils/src/RequestHandlerContainer.php on line 51

In the course of trying to debug I decided to test if what I was passing was callable using the following:

// Regular function to check if something is callable
function isThisCallable( callable $callable  ) {
    call_user_func( $callable );
}

// Modified code from above
static public function registerRoutes( RouteCollector &$r ): void {
    $r->addRoute(['GET', 'OPTIONS'], '/password', function() {
       isThisCallable( array(static::class, 'renderChallenge') );
    } );
}

This test code ran as expected and called the static object's method. Is this a bug or is there another way I should be passing an object and method as the handler?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions