|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-04-19 18:34 UTC] deleet at sapo dot pt
Description:
------------
Possible problem with the implementation of namespaces and late static binding.
Reproduce code:
---------------
<?php
namespace Something;
class Testing {
function __construct() {
}
static function stuff() {
echo 'I work!' . "<br />\n";
}
}
Testing::stuff();
$var = 'Testing'; $func = 'stuff';
// No effect: use Something;
$var::$func();
?>
Expected result:
----------------
Output:
------------------
I work!
I work!
Actual result:
--------------
Output:
------------------
I work!
Fatal error: Class 'Testing' not found in //file url// on line 20
------------------
PHP is not looking for the class inside the defined namespace when using late static binding. It works if I use $var = 'Something::Testing'; but that's not indented behaviour. Also, using the namespace seems to have no effect.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 05 22:00:02 2025 UTC |
This is not a bug, for this simple reason. Let's take these two scripts: file1.php: <?php namespace One; class Testing { function __construct() { } static function stuff() { echo 'I work!' . "<br />\n"; } } $a = 'Testing'; ?> $file2.php: <?php namespace Two; class Testing { function __construct() { } static function stuff() { echo 'I am sinister!' . "<br />\n"; } } ?> file3.php: <?php namespace Two; include 'file1.php'; include 'file2.php'; echo $a; $a::stuff(); ?> What should the output be? There is no deterministic way to do this. Dynamic class references always must be the fully qualified classname, even within the namespace. The same is true of doing: $a = 'Testing'; $b = new $a; and this is by design, as I understand it. The easy way out is to use $a = __NAMESPACE__ . '::Testing'; as this will allow renaming the namespace without penalty.