When extending a class from another namespace that should instantiate a class from within the current namespace, you need to pass on the namespace.
<?php // File1.php
namespace foo;
class A {
public function factory() {
return new C;
}
}
class C {
public function tell() {
echo "foo";
}
}
?>
<?php // File2.php
namespace bar;
class B extends \foo\A {}
class C {
public function tell() {
echo "bar";
}
}
?>
<?php
include "File1.php";
include "File2.php";
$b = new bar\B;
$c = $b->factory();
$c->tell(); // "foo" but you want "bar"
?>
You need to do it like this:
When extending a class from another namespace that should instantiate a class from within the current namespace, you need to pass on the namespace.
<?php // File1.php
namespace foo;
class A {
protected $namespace = __NAMESPACE__;//此处是重点1,这样可以指定到当前命名空间中的对应类
public function factory() {
$c = $this->namespace . '\C';//此处是重点2,这样可以指定到当前命名空间中的对应类
return new $c;
}
}
class C {
public function tell() {
echo "foo";
}
}
?>
<?php // File2.php
namespace bar;
class B extends \foo\A {
protected $namespace = __NAMESPACE__;
}
class C {
public function tell() {
echo "bar";
}
}
?>
<?php
include "File1.php";
include "File2.php";
$b = new bar\B;
$c = $b->factory();
$c->tell(); // "bar"
?>
(it seems that the namespace-backslashes are stripped from the source code in the preview, maybe it works in the main view. If not: fooA was written as \foo\A and barB as bar\B)
PHP手册-__NAMESPACE__关键字(命名空间中继承其他命名空间中类注意)
常量__ NAMESPACE__的值是包含当前命名空间名称的字符串。在全局的,不包括在任何命名空间中的代码,它包含一个空的字符串。