note 116370 added to language.namespaces.rules
| From: | llmll | Date: | Sun, 21 Dec 2014 13:05:42 +0000 |
| Subject: | note 116370 added to language.namespaces.rules | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201233@lists.php.net to get a copy of this message | ||
The mentioned filesystem analogy fails at an important point:
Namespace resolution *only* works at declaration time. The compiler fixates all namespace/class
references as absolute paths, like creating absolute symlinks.
You can't expect relative symlinks, which should be evaluated during access -> during PHP
runtime.
In other words, namespaces are evaluated like __CLASS__ or self:: at parse-time. What's *not*
happening, is the pendant for late static binding like static:: which resolves to the current class
at runtime.
So you can't do the following:
namespace Alpha;
class Helper {
public static $Value = "ALPHA";
}
class Base {
public static function Write() {
echo Helper::$Value;
}
}
namespace Beta;
class Helper extends \Alpha\Helper {
public static $Value = 'BETA';
}
class Base extends \Alpha\Base {}
\Beta\Base::Write(); // should write "BETA" as this is the executing namespace context at
runtime.
If you copy the write() function into \Beta\Base it works as expected.
----
Server IP: 72.52.91.14
Probable Submitter: 185.35.216.238
----
Manual Page -- http://php.net/manual/en/language.namespaces.rules.php
Edit -- https://master.php.net/note/edit/116370
Del: integrated -- https://master.php.net/note/delete/116370/integrated
Del: useless -- https://master.php.net/note/delete/116370/useless
Del: bad code -- https://master.php.net/note/delete/116370/bad+code
Del: spam -- https://master.php.net/note/delete/116370/spam
Del: non-english -- https://master.php.net/note/delete/116370/non-english
Del: in docs -- https://master.php.net/note/delete/116370/in+docs
Del: other reasons-- https://master.php.net/note/delete/116370
Reject -- https://master.php.net/note/reject/116370
Search -- https://master.php.net/manage/user-notes.php