Bug #67547 [Nab]: New feature working for undeclared classes
| From: | requinix@php.net | Date: | Mon, 30 Jun 2014 17:59:23 +0000 |
| Subject: | Bug #67547 [Nab]: New feature working for undeclared classes | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-186400@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67547&edit=1
ID: 67547
Updated by: requinix@php.net
Reported by: magnusthorek at gmail dot com
Summary: New feature working for undeclared classes
Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: N/A
PHP Version: 5.5.14
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2014-06-30 17:25:18] 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:01:31] 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 16:21:39] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67547&edit=1