flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.62k stars 409 forks source link

Uncaught TypeError: flight\Engine::_route(): Argument #2 ($callback) must be of type callable, array given #478

Closed mattfletcher closed 8 months ago

mattfletcher commented 1 year ago

I'm trying to use a simple route, adapted from the docs, to route requests to / to a controller method, and I get errors on PHP 8.1. Works on 7.4:

<?php

use Trains\Controllers\Index;

require __DIR__ . '/../vendor/autoload.php';

Flight::route('/', [Index::class, 'index']);

Flight::start();

NOTICE: PHP message: PHP Fatal error: Uncaught TypeError: flight\Engine::_route(): Argument #2 ($callback) must be of type callable, array given, called in /application/vendor/mikecao/flight/flight/core/Dispatcher.php on line 227 and defined in /application/vendor/mikecao/flight/flight/Engine.php:443

dieter-s commented 1 year ago

Did you create a new instance of the class before? You can check if the class is callable with the PHP is_callable function. If you get false as result, there is no instance of your class, to which flight can point to.

paxperscientiam commented 1 year ago

@mattfletcher

Flight requires fully qualified class references. IE: replace Index::class with Trains\Controllers\Index::class.

As a last resort, one could disable deprecation warnings. EG error_reporting(error_reporting() & ~E_DEPRECATED);

EDIT: Also, the invocation you're using will only work if Index::index is a static function. If it's not a static function, the first item in the array should be a class instance.

krmu commented 8 months ago

Looks like this also has been done @n0nag0n .

n0nag0n commented 8 months ago

Thanks @krmu