search
Homephp教程php手册PHP命名空间(namespace)的动态访问及使用技巧

上篇文章介绍了PHP命名空间的一些术语,其解析规则,本文我们来继续讲述PHP命名空间动态访问及使用技巧,希望能有所帮助

PHP的命名空间(namespace)是PHP 5.3中加入最重要的一个新特性,,这个概念在C#中已经很早就有了,php中的namespace其实和c#的概念是一样的。

一.动态访问命名空间的元素

namespace me\poet; function test() { echo '1111'; } $fun = 'test';//不能这么用,最后$fun()无法动态调用到test():Fatal error: Call to undefined function test() $fun = '\me\poet\test';//正确 //$fun = 'me\poet\test';//正确 $fun();

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


二.魔术常量 和 操作符

namespace me\poet; function test() { echo '1'; } echo __NAMESPACE__; //魔术常量:命名空间的名称(输出 me\poet) //namespace操作符:显式访问当前命名空间或子命名空间中的元素,等价于类中的self操作符 \me\poet\test(); namespace\test(); //上两行代码等价。


三.别名、导入 和 全局空间(含多个例子)

namespace ws\weichen\www; use ws\weichen\www as poet;//定义别名poet //use ws\weichen\www; //不加as,则取最后的作为别名(www) function demo() { echo '1'; } \ws\weichen\www\demo(); poet\demo(); //www\demo();    //不加as的情况,则这样调用

以上三行代码效果一样。
按规则(ws\weichen\www)命名的好处:若更换域名,只要把前缀名称改了就可以,不影响后面代码中别名www的使用。

/* 导入 */ include 'hello.class.php'; use \ws\weichen\www; use \Hello; /*--------------------------------------------------------*/ /* 支持多个use语句 */ use \nihao\shijie as hello, \ws\weichen\www; /*--------------------------------------------------------*/ /* 全局空间:反斜线调用 */ namespace A\B\C; //这个函数是 A\B\C\fopen(); function fopen() { $f = \fopen('demo.txt');//调用全局fopen函数 return $f; }

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)