php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44782 Problem with namespace and dynamic referencing
Submitted: 2008-04-19 18:34 UTC Modified: 2008-04-19 23:40 UTC
From: deleet at sapo dot pt Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3CVS-2008-04-19 (snap) OS: Fedora 6
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: deleet at sapo dot pt
New email:
PHP Version: OS:

 

 [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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-19 21:43 UTC] colder@php.net
This isn't late static bindings, but it's indeed a bug, probably relying on the fact that Testing::$func(); would have been rewritten in compile time.

 [2008-04-19 22:14 UTC] deleet at sapo dot pt
Very well, dynamic referencing it is then :)

Thank you for the quick reply, is it possible to get an ETA on the fix?
 [2008-04-19 23:40 UTC] cellog@php.net
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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 28 16:00:02 2025 UTC