开发者

Namespace and classes in php

开发者 https://www.devze.com 2022-12-12 22:56 出处:网络
Wh开发者_StackOverflowy i receive error? the class in the same namespace.. php 5.3.0 namespace ExampleSystem\\Core;

Wh开发者_StackOverflowy i receive error? the class in the same namespace..

php 5.3.0

namespace ExampleSystem\Core;
class Test {
    public function __construct() {
        print 'Test ok';
    }
}

// Fatal error: Class 'Test' not found in ...
$class_name = 'Test';
$obj = new $class_name;

// Ok
$class_name = 'ExampleSystem\Core\Test';
$obj = new $class_name;

// Ok
$obj = new Test;


I can't find chapter and verse in the PHP manual, but the obvious explanation is that when you do:

 $obj = new $string

then the value of $string is not mapped into the current namespace. This makes sense, when you consider that $string may have been passed in from somewhere else, where a different namespace may have been in effect.

0

精彩评论

暂无评论...
验证码 换一张
取 消