Home  >  Article  >  php教程  >  [namespace]PHP命名空间的动态访问 & 使用技巧

[namespace]PHP命名空间的动态访问 & 使用技巧

WBOY
WBOYOriginal
2016-06-06 19:49:27888browse

----------------------------------------------------------------------------------------------- /* |----------------------------------------|一.动态访问命名空间的元素|@黑眼诗人 www.farwish.com|---------------------------------------- */ na

-----------------------------------------------------------------------------------------------

<span>/*</span><span>
|----------------------------------------
|一.动态访问命名空间的元素
|@黑眼诗人 <www.farwish.com>
|----------------------------------------
</www.farwish.com></span><span>*/</span><span>
namespace me\poet;

</span><span>function</span><span> test()
{
    </span><span>echo</span> '1111'<span>;
}

</span><span><span>$fun</span> = 'test';</span><span>//</span><span>不能这么用,最后$fun()无法动态调用到test():<strong>Fatal error</strong>: Call to undefined function test()</span>

<span>$fun</span> = '\me\poet\test';<span>//</span><span>正确

//$fun = 'me\poet\test';//正确</span>

<span>$fun</span><span>();<br>

</span><span>//</span><span>也就是说,动态调用必须是 限定名称 或 完全限定名称(概念参考:PHP命名空间的使用基础)</span>

 

<span>/*</span><span>
|-------------------------------------
|二.魔术常量 和 操作符
|-------------------------------------
</span><span>*/</span><span>
namespace me\poet;

</span><span>function</span><span> test()
{
    </span><span>echo</span> '1'<span>;
}

</span><span>echo</span> __NAMESPACE__; <span>//</span><span>魔术常量:命名空间的名称(输出 me\poet)

//namespace操作符:显式访问当前命名空间或子命名空间中的元素,等价于类中的self操作符</span>
\<span>me\poet\test();

namespace\test();

//上两行代码等价。</span>

 

<span>/*</span><span>
|-------------------------------------------
|三.别名、导入 和 全局空间(含多个例子)
|-------------------------------------------
</span><span>*/</span><span>
namespace ws\weichen\www;

</span><span>use</span> ws\weichen\www <span>as</span> poet;<span>//</span><span>定义别名poet

//use ws\weichen\www; //不加as,则取最后的作为别名(www)</span>

<span>function</span><span> demo()
{
    </span><span>echo</span> '1'<span>;
}

\ws\weichen\www\demo();

poet\demo();

</span><span>//</span><span>www\demo();    //不加as的情况,则这样调用

//以上三行代码效果一样。

//按规则(ws\weichen\www)命名的好处:若更换域名,只要把前缀名称改了就可以,不影响后面代码中别名www的使用。</span>

/*--------------------------------------------------------*/<br>
<span>/*</span><span> 导入 </span><span>*/<br></span>
<span>include</span> 'hello.class.php'<span>;

</span><span>use</span><span> \ws\weichen\www;

</span><span>use</span><span> \Hello;
<br>/*--------------------------------------------------------*/<br>
</span><span>/*</span><span> 支持多个use语句 </span><span>*/<br></span>
<span>use</span> \nihao\shijie <span>as</span> hello,<span> \ws\weichen\www;

/*--------------------------------------------------------*/<br>
</span><span>/*</span><span> 全局空间:反斜线调用 </span><span>*/</span><span>
<br>namespace A\B\C;
</span><span>//</span><span>这个函数是 A\B\C\fopen();</span>
<span>function</span> <span>fopen</span><span>()
{
    </span><span>$f</span> = \<span>fopen</span>('demo.txt');<span>//</span><span>调用全局fopen函数</span>
    <span>return</span> <span>$f</span><span>;
}</span>

 

Link: http://www.cnblogs.com/farwish/p/3913784.html

@黑眼诗人 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP 验证邮箱是否合法,正确Next article:PHP中