php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67547 New feature working for undeclared classes
Submitted: 2014-06-30 16:21 UTC Modified: 2014-06-30 17:59 UTC
From: magnusthorek at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.5.14 OS: N/A
Private report: No CVE-ID: None
 [2014-06-30 16:21 UTC] magnusthorek at gmail dot com
Description:
------------
PHP 5.5 has implemented as a new feature a new way to retrieve the classname through the syntax ::class:

It work ver well as described in "Sample #1" below. But why this syntax also returns a classname when used alongside an undeclared class, according to "Sample #2"?

In several other cases an error is raised, but not here. Anyone know, with concrete basis if possible, why does this happen?

Does it have anything to Late Static Bindings or it's just a (temporary) limitation/bug of this brand new feature?

Test script:
---------------
<?php

// Sample #1

namespace Testing;

class Test{}

echo Test::class; // Testing\Test;

?>

<?php

// Sample #2

echo UndeclaredClass::class; // UndeclaredClass

Expected result:
----------------
For "Sample #1":

Testing\test;

For Sample #2:

Fatal error: Class 'UndeclaredClass'...

Actual result:
--------------
For "Sample #1":

Testing\test;

For Sample #2:

UndeclaredClass

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-06-30 17:01 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-06-30 17:01 UTC] requinix@php.net
PHP doesn't actually need the definition of the class to know its fully-qualified name - all the required information is available at compile-time. So it doesn't try to load it.
 [2014-06-30 17:25 UTC] magnusthorek at gmail dot com
I understood you perfectly, however this subject were raised by several different people debating about it and I would like to share this knowledge with them. So, would you mind to provide a little more elaborated version of this explanation?
 [2014-06-30 17:59 UTC] requinix@php.net
This isn't really an appropriate place for a discussion so I'll keep this brief. Try a mailing list or forum for something more in-depth.
http://www.php.net/support.php

Namespaces and uses are resolved at compile-time (ie, when PHP "compiles" the file before execution). That's why there are strict requirements about how they can be used. Because of those requirements, when PHP encounters a class name it can immediately know the fully-qualified name. Think of it as a filesystem: the namespace is like a directory for relative locations and the use statements are like symlinks.

The class name is either absolute ("\Testing\Test") or relative ("Test"), and if relative it could be a normal name

  namespace Testing {
    echo Test::class; // \Testing + Test = \Testing\Test
  }

or an alias

  use Testing\Test as AliasedTest;
  echo AliasedTest::class; // AliasedTest + use = \Testing\Test

Remember autoloading? Without all this, autoloading wouldn't work!

::class is just a new tool to expose information PHP has always known.
 [2014-06-30 18:51 UTC] magnusthorek at gmail dot com
Thank you very much. Your time clarified one point for two different communities.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Sep 16 00:01:27 2019 UTC