Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

short method references #2

Open
olivernybroe opened this issue Sep 11, 2019 · 4 comments
Open

short method references #2

olivernybroe opened this issue Sep 11, 2019 · 4 comments
Labels
rfc New proposal

Comments

@olivernybroe
Copy link

So in PHP we can make a method reference by using this syntax right now

Route::get('users', [UserController::class, 'index']);

I propose that we add a short method reference syntax like so

Route::get('users', UserController::index);
@nunomaduro nunomaduro added the rfc New proposal label Sep 11, 2019
@nunomaduro nunomaduro changed the title [RFC] Short method references short method references Sep 11, 2019
@stancl
Copy link

stancl commented Sep 11, 2019

Should this work only with static access? It could be problematic with access inside the class because you can have a property and a method with the same name. $this->foo() and ($this->foo)() are different things.

And with static methods, this is already possible. Strings that reference static methods are callable:

>>> class Bar { public static function xyz() { return 'foo'; } }
>>> 'Bar::xyz'();
=> "foo"

@olivernybroe
Copy link
Author

@stancl This should work with non static method also. It should not be used for properties, only methods.
As in my example, it would basically just be a wrapper for [Bar::class, 'xyz'], as that syntax also supports static methods.

>>> class Bar { public static function xyz() { return 'foo'; } }
>>> [Bar::class, 'xyz']();
=> "foo"

The only "problem" I really see with it is that the same syntax is used for constants. However in that case, I think the constant should have priority.

@enumag
Copy link

enumag commented Sep 12, 2019

The only "problem" I really see with it is that the same syntax is used for constants. However in that case, I think the constant should have priority.

In real world constants are usually written in uppercase and methods in camelcase so it's not a real problem. In my opinion Plus should simply throw an error if a class has both a method and a constant with the same name.

@olivernybroe
Copy link
Author

@enumag Yeah, I like the idea of just throwing an error. And the uppercase/camelcase scenario was also why I sad "problem" 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
rfc New proposal
Projects
None yet
Development

No branches or pull requests

4 participants