php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70582 Referncing NotExistingClass::class should trigger error/warning/notice
Submitted: 2015-09-25 14:18 UTC Modified: 2015-09-26 06:58 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: andreas dot stangl at stansotec dot de Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: Next Major Version OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: andreas dot stangl at stansotec dot de
New email:
PHP Version: OS:

 

 [2015-09-25 14:18 UTC] andreas dot stangl at stansotec dot de
Description:
------------
When referencing the "magic" class constant "::class" the interpreter does not care if the given class does not exist.

If I have e.g. a class "My\FancyClass" and I access the "class" constant like

    echo My\FancyClass::class;

I get

    "My\FancyClass"

(which is pretty cool especially for unit tests). But if e.g. My\FancyClass gets renamed to "My\FoobarClass" the interpreter will not complain at all.

But referencing any other (not existing) constant of a not existing class like

    echo My\FoobarClass::blah;

Produces a fatal error. This looks a little inconsistent to me.


Test script:
---------------
<?php
namespace My;

class FancyClass {}

echo FancyClass::class . "\n";        // useful
echo NotExistingClass::class . "\n";  // imho not so useful :-/
echo NotExistingClass::blah;          // fatal error

Expected result:
----------------
My\FancyClass
PHP Fatal error:  Class 'My\NotExistingClass' not found in testScript.php on line 7

Actual result:
--------------
My\FancyClass
My\NotExistingClass
PHP Fatal error:  Class 'My\NotExistingClass' not found in testScript.php on line 8

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-09-25 14:24 UTC] andreas dot stangl at stansotec dot de
-Package: PHP Language Specification +Package: Class/Object related
 [2015-09-25 14:24 UTC] andreas dot stangl at stansotec dot de
Changed "Package" to "Class/Object related".
 [2015-09-25 23:03 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug
 [2015-09-25 23:03 UTC] requinix@php.net
::class is just a fancy way of getting the class's fully-qualified name without having to hard-code it into your application. It's a step-up from having to write "My\\FancyClass". I suggest using an IDE which can do things like search for symbols (and not just mere strings) because that means that before you rename a class you can search for its old name and will find the FancyClass::class usage - which wouldn't be as quick with the class name as a string.

To the point, PHP does not need to autoload the class to determine its name: the only thing that affects the name is the current namespace and any use declarations present, which it already has on hand. Autoloading would unnecessarily slow down execution.

Regarding the inconsistency, all magic constants have special behavior - that's what makes them "magic". __FILE__ and __LINE__ and such are magical in that their values are not constant, and if you try to compare them in two different places then you won't get the regular behavior (equality). ::class is magical in that it is not literally defined on the class, and if you try to use it on a class that does not exist then you won't get the regular behavior (a fatal error).
 [2015-09-26 06:58 UTC] andreas dot stangl at stansotec dot de
Ok. So this is basically a "won't fix"? Then I guess I'll have to do it like before PHP 5.5

trait GetClassNameTrait
{
    public static function getClassName()
    {
        return get_called_class();
    }
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 23:01:28 2024 UTC