Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SomeClass::class doesn't return fully qualified class name when class is aliased #64

Closed
vincemukiiri opened this issue Dec 10, 2018 · 9 comments

Comments

@vincemukiiri
Copy link

If I have a class App\User and I type User::class in the artisan console, it first outputs [!] Aliasing 'User' to 'App\User' for this Tinker session. then returns User instead of App\User.

This leads to unexpected behaviour in certain situations in tinker like when authorizing using policies $user->can('create', User::class) will always return false

@vincemukiiri vincemukiiri changed the title SomeClass::class doesn't return fully qualified class name when class is aliasing SomeClass::class doesn't return fully qualified class name when class is aliased Dec 10, 2018
@driesvints
Copy link
Member

This is unfortunately how it works. You could use the full namespace or use the alias blacklist to prevent classes from being aliased: https://laravel.com/docs/5.7/artisan#tinker

@brysonreece
Copy link

Wouldn't this still be considered unexpected behavior? If the output to the user is that the short class name is aliased to the fully qualified class name, then the behavior of Tinker should behave such that it was indeed aliased.

What's the point in aliasing one value to another if the original value doesn't return the data it was mapped to?

@driesvints
Copy link
Member

@brysonreece because aliasing wasn't meant to be used in combination with a full app lifecycle. If you want to run code from your app itself inside Tinker then you'd best use the full namespace.

Aliasing was meant to do some quick data retrieval, etc by just using the class name.

@brysonreece
Copy link

I understand, however the larger issue is that it is still unexpected behavior that conflicts with what the user is being told. Can the message at least be amended to reflect that better or at least be documented somewhere?

@brysonreece
Copy link

Plus it's not hard to encounter this issue without requiring interaction of a full app lifecycle.

@driesvints
Copy link
Member

@brysonreece I'm not sure how the message can be more clear. It's already pretty clear what it means to me.

@brysonreece
Copy link

It may be a matter of semantics but I think it's easy to misinterpret "how" the requested class is aliased to the fully qualified class name. Aliasing is usually assumed that one value maps to another, like the user is being informed of. However when the value is requested again, in this case, it's not returning what they were informed it was actually "aliased" to, which leads to unexpected behavior.

@driesvints
Copy link
Member

@brysonreece maybe a note to the docs? Feel free to send in a PR if you want to.

@Whizboy-Arnold
Copy link

hello to have the thing return the full class please import first, if in your code

otherwise in artisan alias manually like
use App\Models\User as AliasUser;

AliasUser::class; ///gives the correct App\Models\User instead of User as if you just do User::class, this is as seems tinker aliases for you it only does so when you use the class not just when you want to echo the class.
consider writing say in your code
echo User::class; without first importing User
it will echo just fine without any errors but it will just echo 'User' while what you want is App\Models\User
usually this is important in mapping polymorphic relationships
so correct use of it to give fully qualified name would rather be:

use App\Models\User;
echo User::class;

its more of a php issue than a laravel as php ideally should not just return the class name you give it when you have not imported it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants