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

Inconsistency about ::class and literal strings #1646

Open
ondrejmirtes opened this issue Jun 16, 2022 · 4 comments
Open

Inconsistency about ::class and literal strings #1646

ondrejmirtes opened this issue Jun 16, 2022 · 4 comments
Labels
bug Documentation contains incorrect information Category: Engine Status: Needs Triage

Comments

@ondrejmirtes
Copy link
Contributor

Description

This doesn't work (https://3v4l.org/MM1pJ):

<?php

$foo = 'Foo';
var_dump($foo::class);

But this does (https://3v4l.org/Rb3oU)

<?php

var_dump('Foo'::class);

Is this intentional? I'd say both or neither should be supported.

PHP Version

Irrelevant

Operating System

N/A

@cmb69
Copy link
Contributor

cmb69 commented Jun 16, 2022

I think the behavior is intentional. On one hand, ::class is basically accessing a class constant, so generally that can be done on the class name string, regardless of given as literal or variable. However, ::class is resolved during compile time, so supporting variables is generally impossible.

And while I don't see any value in supporting a class name string here, not supporting it would be some arbitrary restriction. Furthermore, changing that would constitute a BC break.

So I think we should improve the documentation.

@iluuu1994
Copy link
Member

It seems the 'Foo'::class syntax was supported even before https://wiki.php.net/rfc/class_name_literal_on_object. That RFC does mention specifically that dynamic strings aren't allowed, so this seems to be a documentation issue.

@cmb69
Copy link
Contributor

cmb69 commented Jun 23, 2022

Okay, no further input, so I'm transferring to doc-en.

@Girgias
Copy link
Member

Girgias commented Jul 25, 2023

Somewhat related to #2138

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Documentation contains incorrect information Category: Engine Status: Needs Triage
Projects
None yet
Development

No branches or pull requests

4 participants