Req->Bug #79085 [Opn->Nab]: Incosistency on ::class constant

From: Date: Wed, 08 Jan 2020 19:38:40 +0000
Subject: Req->Bug #79085 [Opn->Nab]: Incosistency on ::class constant
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-224791@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
 Updated by:         requinix@php.net
 Reported by:        almamu at almamu dot com
 Summary:            Incosistency on ::class constant
-Status:             Open
+Status:             Not a bug
-Type:               Feature/Change Request
+Type:               Bug
 Package:            Class/Object related
 Operating System:   Any
 PHP Version:        7.2.26
 Block user comment: N
 Private report:     N

 New Comment:

::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.


Previous Comments:
------------------------------------------------------------------------
[2020-01-08 19:18:05] almamu at almamu dot com

Description:
------------
I've noticed some inconsistencies on how the ::class magic constant is handled compared to
other constants/class members. As seen in the example script, if you try to access ::class on a
non-existant class, you don't get any kind of error, which would be expected, as the class
itself doesn't exist. This can provoke some nasty errors where you typo the class' name
and PHP doesn't even give you a (expected) warning.

I've created an interactive example with that same code so you can check it live and toy with
it: https://3v4l.org/RbIhK

Test script:
---------------
<?php
 echo FOO::class;
 echo FOO::constantName;
 echo FOO::$username; // It works as echoing Typo\WTF; It shall fail...

Expected result:
----------------
    Fatal error: Uncaught Error: Class 'FOO' not found in /in/RbIhK:2
    Stack trace:
    #0 {main}
      thrown in /in/RbIhK on line 3

    Process exited with code 255.

Actual result:
--------------
    FOO
    Fatal error: Uncaught Error: Class 'FOO' not found in /in/RbIhK:3
    Stack trace:
    #0 {main}
      thrown in /in/RbIhK on line 3

    Process exited with code 255.


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



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


Thread (8 messages)

« previous php.bugs (#224791) next »