laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

Callback in routes does not support object method callable type #2481

Closed maduranma closed 3 years ago

maduranma commented 3 years ago

I opened this issue: https://github.com/laravel/framework/issues/36079

It is not supported, adding support would be perfect as it's essential for some programming paradigms.

Description:

In the routing, you cannot pass a callable array formed by an object and a string (function name), even this is a supported callable.

PHP manual: https://www.php.net/manual/en/language.types.callable.php (type 3)

You can try it like this:

class foo
{
    public function bar()
    {
        echo 'This is a test';
    }
}

$callback = [new foo, 'bar'];
$callback(); // It echoes "This is a test"

Steps To Reproduce:

In the routes file:

class foo {
    public function bar() {
        echo 'This is a test';
    }
}
Route::get(
    '/test',
    [new foo, 'bar']
);

This throws an error in Illuminate\Routing\RouteAction as in line 34 it tries to convert the object to a string when concatenating for uses and controller in return array.

themsaid commented 3 years ago

Feel free to open a PR with your suggested changes.