Namespace and classes in php
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.
精彩评论