#46310 [Opn->Bgs]: Namespace resolution is not done when creating objects of a variable class name
| From: | cellog@php.net | Date: | Thu, 16 Oct 2008 04:00:37 +0000 |
| Subject: | #46310 [Opn->Bgs]: Namespace resolution is not done when creating objects of a variable class name | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-129744@lists.php.net to get a copy of this message | ||
ID: 46310
Updated by: cellog@php.net
Reported By: benno at transmog dot com dot au
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Linux, but probably irrelevant
PHP Version: 5.3.0alpha2
New Comment:
When you do this code:
<?php
namespace ns;
$a = new blah;
?>
PHP automatically "expands" it to:
<?php
$a = new ns::blah;
?>
The same is true at definition time. When you do this code:
<?php
namespace ns;
class classname{}
?>
PHP automatically expands it to:
<?php
class ns::classname{}
?>
In other words, if you want to access "ns::classname" with a dynamic
call, you'll need to perform this expansion manually:
$a = __NAMESPACE__ . '::' . $classname;
return new $a();
$a = "testclass";
$b = new $a();
will always refer to the globally un-namespaced:
<?php
class testclass {}
?>
Previous Comments:
------------------------------------------------------------------------
[2008-10-16 01:28:42] benno at transmog dot com dot au
Description:
------------
Suppose you have a namespace that contains:
- a function that has "new $blah ();"
- the class $blah
The instantiation will generate a fatal error if the namespace is not
explicitly specified. I assume then that namespace resolution is not
done when instantiating an object of a variable class name.
This is similar to Bug #45197, but I don't think it's quite the same
thing. Classes must know, at runtime, which namespace they belong to for
the runtime namespace resolution described in the manual to work -
http://php.net/manual/en/language.namespaces.definition.php
Please advise either way.
Reproduce code:
---------------
Classes (defined in namespace ns):
http://transmog.com.au/php/ns.phps
Test suite:
http://transmog.com.au/php/test.phps
Expected result:
----------------
a: ok
b: ok
c: ok
Actual result:
--------------
a: ok
b: ok
c: <br />
<b>Fatal error</b>: Class 'testclass' not found in
<b>/var/www/ns.php</b> on line <b>7</b><br />
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46310&edit=1