php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47057 Inconsitent namespace resolving when using dynamic class instanciation
Submitted: 2009-01-09 17:39 UTC Modified: 2009-01-11 09:45 UTC
From: tobias dot john at fondsnet dot de Assigned: dmitry (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3CVS-2009-01-09 (CVS) OS: Mac OS X 10.5
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tobias dot john at fondsnet dot de
New email:
PHP Version: OS:

 

 [2009-01-09 17:39 UTC] tobias dot john at fondsnet dot de
Description:
------------
When using a variable to instantiate a class, the behavior in namespace resolving is inconsistent to the behavior when using a static class name.

Reproduce code:
---------------
namespace foo\bar;

class Baz {
    public $value;

    public function __construct($v) {
        $this->value = $v;
    }

    public function echoValue() {
        echo 'Value: ' . $this->value, PHP_EOL;
    }
}

$classname1 = '\foo\bar\Baz';
$classname2 = 'foo\bar\Baz';

$a = new $classname1('A');
$a->echoValue();
$b = new $classname2('B');
$b->echoValue();
$c = new \foo\bar\Baz('C');
$c->echoValue();
$d = new foo\bar\Baz('D');
$d->echoValue();

Expected result:
----------------
Value: A

Fatal error: Class 'foo\bar\foo\bar\Baz' not found in /Users/tj/Documents/workspace/yeti/lib/yeti/NamespaceTest.php on line 20


Actual result:
--------------
Value: A
Value: B
Value: C

Fatal error: Class 'foo\bar\foo\bar\Baz' not found in /Users/tj/Documents/workspace/yeti/lib/yeti/NamespaceTest.php on line 24


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-01-11 09:45 UTC] dmitry@php.net
Namespaces is a compile time feature. In run-time you should always use full qualified names. You can use a special constant __NAMESPACE__ to do it.

$classname2 = __NAMESPACE__ . '\foo\bar\Baz';

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 23:01:33 2025 UTC