php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46310 Namespace resolution is not done when creating objects of a variable class name
Submitted: 2008-10-16 01:28 UTC Modified: 2008-10-16 04:00 UTC
From: benno at transmog dot com dot au Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.0alpha2 OS: Linux, but probably irrelevant
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: benno at transmog dot com dot au
New email:
PHP Version: OS:

 

 [2008-10-16 01:28 UTC] 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 />


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-16 04:00 UTC] cellog@php.net
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 {}
?>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 05:01:30 2024 UTC