PHP手册-__NAMESPACE__关键字(命名空间中继承其他命名空间中类注意)
来源:互联网 发布:xlplayer mac有杂音 编辑:程序博客网 时间:2024/05/26 05:53
常量__ NAMESPACE__的值是包含当前命名空间名称的字符串。在全局的,不包括在任何命名空间中的代码,它包含一个空的字符串。
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__关键字(命名空间中继承其他命名空间中类注意)
- PHP中命名空间
- PHP手册-命名空间
- PHP手册-命名空间
- PHP中使用命名空间
- C#中命名空间关键字:namespace、using
- C++中命名空间
- C++中命名空间
- PHP 中命名空间的作用
- PHP 中命名空间的作用
- 如何理解php中命名空间概念
- PHP中命名空间的引用
- php命名空间中别名的使用
- PHP程序中命名空间的使用
- PHP中命名空间不足之详解
- php 中 遇见namespace 命名空间
- 对象,类,命名空间,继承......
- 关键字之命名空间
- 【C#基础语言学习】:const,readonly 常量与只读的区别
- Eclipse 查看源代码
- Java内存分配原理
- 黑马程序员_.NET学习11(书第四章)
- Linux里设置环境变量的方法(export PATH)
- PHP手册-__NAMESPACE__关键字(命名空间中继承其他命名空间中类注意)
- 鼠标消息
- wamp——利用phpmyadmin修改数据库密码
- Linux tail 命令详解
- declare-styleable的使用
- 如何区分引用类型和原始类型
- android 线程更新view及数据传送
- 浏览获得文件夹路径
- 黑马程序员_.NET学习9(第六章)