Skip to content

Support $obj::class #5065

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

Closed
wants to merge 1 commit into from
Closed

Support $obj::class #5065

wants to merge 1 commit into from

Conversation

nikic
Copy link
Member

@nikic nikic commented Jan 8, 2020

RFC: https://wiki.php.net/rfc/class_name_literal_on_object

This allows $obj::class, which gives the same result as get_class($obj). Anything other than an object results in TypeError.

I believe the original argument against this was that ::class is a compile-time feature, but that isn't quite true (e.g. static::class), and people generally expect that $obj::class works.

@nikic nikic added the RFC label Jan 8, 2020
@mvorisek
Copy link
Contributor

@nikic What do you think about the same for class property, like $x->dummy::prop which will return dummy? Can be very, very usefull for larger projects to support refactoring.

@php-pulls php-pulls closed this in d933591 Feb 11, 2020
@carusogabriel carusogabriel added this to the PHP 8.0 milestone May 29, 2020
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Jul 11, 2020
> It is now possible to fetch the class name of an object using `$object::class`. The result is the same as `get_class($object)`.

Refs:
* https://wiki.php.net/rfc/class_name_literal_on_object
* php/php-src#5065
* php/php-src@d933591

This adjusts the existing `PHPCompatibility.Constants.NewMagicClassConstant` sniff to differentiate between `::class` being used on class names/`self`/`parent`/`static`, as allowed per PHP 5.5 and `::class` being used on objects, as allowed per PHP 8.0.

Note: there are a number of syntaxes which are still not allowed (and which don't make any sense anyhow). These would cause a fatal error when used in PHP.

Differentiating between what is allowed and what isn't, is pretty hard though and realistically beyond the capabilities of a static analysis tool as `$var` in `$var::class` could be either an object or a literal, with the first being allowed, while the second isn't.

So I have taken the executive decision that, for now, everything which wasn't yet explicitly allowed per PHP 5.5 will throw an error for PHP < 8, independently of whether it is now supported or still forbidden.

In a way, this is no different from how the sniff behaved before, as previously, using `::class` on any context would throw an error for PHP < 5.5, independently of whether the used syntax was actually supported in PHP 5.5+.

Includes unit tests.

Includes minor fixes to the existing unit tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants