Support $obj::class#5065
Closed
nikic wants to merge 1 commit into
Closed
Conversation
Contributor
|
@nikic What do you think about the same for class property, like $x->dummy::prop which will return |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RFC: https://wiki.php.net/rfc/class_name_literal_on_object
This allows
$obj::class, which gives the same result asget_class($obj). Anything other than an object results inTypeError.I believe the original argument against this was that
::classis a compile-time feature, but that isn't quite true (e.g.static::class), and people generally expect that$obj::classworks.