Req #79085 [Opn]: Incosistency on ::class constant

From: Date: Fri, 10 Jan 2020 10:36:17 +0000
Subject: Req #79085 [Opn]: Incosistency on ::class constant
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-224824@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79085&edit=1

 ID:                 79085
 User updated by:    almamu at almamu dot com
 Reported by:        almamu at almamu dot com
 Summary:            Incosistency on ::class constant
 Status:             Open
 Type:               Feature/Change Request
 Package:            Class/Object related
 Operating System:   Any
 PHP Version:        7.2.26
 Block user comment: N
 Private report:     N

 New Comment:

I see, I completely overlooked the class_alias function, I've always used "use ClassName
as Alias". The second parameter doesn't look like it'd be a good idea to use ::class
to specify it, but for the first parameter it might be an issue to autoload it on certain situations
( that would imply there is no "use ClassName" for it, because that would trigger the
autoload regardless of what the third parameter says. which doesn't really look good to me
either). I guess the same applies to the class_exists function.

Is this kind of usage that common? All the usages I've witnessed of "class_alias" in
the wild leave out the third parameter (and thus enabling the autoload).


Previous Comments:
------------------------------------------------------------------------
[2020-01-10 09:41:29] nikic@php.net

One common usage is in class_alias(). One of the operands will naturally not be declared at the time
of the class_alias() call.

------------------------------------------------------------------------
[2020-01-10 09:40:06] almamu at almamu dot com

I've tried to come up with situations where that might be useful, but I fail to see any
benefits on it. Do you mind sharing an example situation where it is useful?

------------------------------------------------------------------------
[2020-01-09 21:52:30] requinix@php.net

Personally I find it very helpful that I can FOO::class and have it not trigger the autoloader.

------------------------------------------------------------------------
[2020-01-09 11:57:33] almamu at almamu dot com

I understand that's how it works internally, that's why I did create this as
Feature/Change request and not as a bug. I think this should behave consistently as other constants
(because the documentation calls it a special constant: "The special ::class constant is
available as of PHP 5.5.0, and allows for fully qualified class name resolution at compile time,
this is useful for namespaced classes: "), just like there's a request to make it work
with objects: https://wiki.php.net/rfc/class_name_literal_on_object

This RFC makes an important point too, we already support static::class, which is resolved on
runtime and not compiletime.

------------------------------------------------------------------------
[2020-01-08 19:38:39] requinix@php.net

::class is special and not really a constant. The compiler sees it and expands the
"FOO::class" to be a string. As in when that gets executed, it's a literal string
value - like if you had written it right into the code yourself.

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=79085


--
Edit this bug report at https://bugs.php.net/bug.php?id=79085&edit=1


Thread (8 messages)

« previous php.bugs (#224824) next »