search
HomeBackend DevelopmentPHP TutorialPHP命名空间规则解析及高级功能_PHP

日前发布的PHP 5.3中,最重要的一个新特性就是命名空间的加入。本文介绍了PHP命名空间的一些术语,其解析规则,以及一些高级功能的应用,希望能够帮助读者在项目中真正使用命名空间。

在这里中我们介绍了PHP命名空间的用途和namespace关键字,在这篇文章中我们将介绍一下use命令的使用以及PHP如何解析命名空间的名字的。

为了便于对比,我定义了两个几乎一样的代码块,只有命名空间的名字不同。

  1. // application library 1  
  2. namespace App\Lib1;  
  3. const MYCONST = 'App\Lib1\MYCONST';  
  4. function MyFunction() {  
  5.  return __FUNCTION__;  
  6. }  
  7. class MyClass {  
  8.  static function WhoAmI() {  
  9. eturn __METHOD__;  
  10.  }  
  11. }  
  12. ?>

lib2.php

  1. // application library 2  
  2. namespace App\Lib2;  
  3.  
  4. const MYCONST = 'App\Lib2\MYCONST';  
  5.  
  6. function MyFunction() {  
  7.  return __FUNCTION__;  
  8. }  
  9.  
  10. class MyClass {  
  11.  static function WhoAmI() {  
  12. eturn __METHOD__;  
  13.  }  
  14. }  
  15. ?> 

开始之前先要理解几个PHP命名空间相关术语。

◆完全限定名称(Fully-qualified name)

任何PHP代码都可以引用完全限定名称,它是一个以命名空间反斜线开头的标识符,如\App\Lib1\MYCONST,\App\Lib2\MyFunction( )等。

完全限定名称是没有任何歧义的,开头的反斜线和文件路径的作用有点类似,它表示“根”全局空间,如果我们在全局空间中实现了一个不同的MyFunction( ),可以使用\MyFunction( )从lib1.php或lib2.php调用它。

完全限定名称对一次性函数调用或对象初始化非常有用,但当你产生了大量的调用时它们就没有实用价值了,在下面的讨论中我们将会看到,PHP提供了其它选项以解除我们为命名空间打字的烦恼。

◆限定名称(Qualified name)

至少有一个命名空间分隔符的标识符,如Lib1\MyFunction( )。

◆非限定名称(Unqualified name)

没有命名空间分隔符的标识符,如MyFunction( )。

在相同的命名空间内工作

仔细思考下面的代码:

myapp1.php

<ol class="dp-c">
<li class="alt"><span><span></span></span></li>
<li><span>namespace App\Lib1;  </span></li>
<li class="alt"><span> </span></li>
<li>
<span class="keyword"><strong><font color="#006699">require_once</font></strong></span><span>(</span><span class="string"><font color="#0000ff">'lib1.php'</font></span><span>);  </span>
</li>
<li class="alt">
<span class="keyword"><strong><font color="#006699">require_once</font></strong></span><span>(</span><span class="string"><font color="#0000ff">'lib2.php'</font></span><span>);  </span>
</li>
<li><span> </span></li>
<li class="alt">
<span>header(</span><span class="string"><font color="#0000ff">'Content-type: text/plain'</font></span><span>);  </span>
</li>
<li>
<span class="func">echo</span><span> MYCONST . </span><span class="string"><font color="#0000ff">"\n"</font></span><span>;  </span>
</li>
<li class="alt">
<span class="func">echo</span><span> MyFunction() . </span><span class="string"><font color="#0000ff">"\n"</font></span><span>;  </span>
</li>
<li>
<span class="func">echo</span><span> MyClass::WhoAmI() . </span><span class="string"><font color="#0000ff">"\n"</font></span><span>;  </span>
</li>
<li class="alt"><span>?>  <br></span></li>
</ol>

即使我们同时包括了lib1.php和lib2.php,MYCONST,MyFunction和MyClass标识符只能在lib1.php中引用,这是因为myapp1.php的代码在相同的App\Lib1命名空间内。

执行结果:

  1. App\Lib1\MYCONST  
  2. App\Lib1\MyFunction  
  3. App\Lib1\MyClass::WhoAmI  

命名空间导入

可以使用use操作符导入命名空间,如:

myapp2.php

 
  1. use App\Lib2;  
  2.  
  3. require_once('lib1.php');  
  4. require_once('lib2.php');  
  5.  
  6. header('Content-type: text/plain');  
  7. echo Lib2\MYCONST . "\n";  
  8. echo Lib2\MyFunction() . "\n";  
  9. echo Lib2\MyClass::WhoAmI() . "\n";  
  10. ?>  

可以定义任意数量的use语句,或使用逗号分隔成独立的命名空间,在这个例子中我们导入了App\Lib2命名空间,但我们仍然不能直接引用 MYCONST,MyFunction和MyClass,因为我们的代码还在全局空间中,但如果我们添加了“Lib2\”前缀,它们就变成限定名称 了,PHP将会搜索导入的命名空间,直到找到匹配项。

执行结果:

  1. App\Lib2\MYCONST  
  2. App\Lib2\MyFunction  
  3. App\Lib2\MyClass::WhoAmI 

命名空间别名

命名空间别名可能是最有用的构想了,别名允许我们使用较短的名称引用很长的命名空间。

myapp3.php

  1. use App\Lib1 as L;  
  2. use App\Lib2\MyClass as Obj;  
  3.  
  4. header('Content-type: text/plain');  
  5. require_once('lib1.php');  
  6. require_once('lib2.php');  
  7.  
  8. echo L\MYCONST . "\n";  
  9. echo L\MyFunction() . "\n";  
  10. echo L\MyClass::WhoAmI() . "\n";  
  11. echo Obj::WhoAmI() . "\n";  
  12. ?>  

第一个use语句将App\Lib1定义为“L”,任何使用“L”的限定名称在编译时都会被翻译成“App\Lib1”,因此我们就可以引用L\MYCONST和L\MyFunction而不是完全限定名称了。

第二个use语句定义了“obj”作为App\Lib2\命名空间中MyClass类的别名,这种方式只适合于类,不能用于常量和函数,现在我们就可以使用new Obj( )或象上面那样运行静态方法了。

执行结果:

  1. App\Lib1\MYCONST  
  2. App\Lib1\MyFunction  
  3. App\Lib1\MyClass::WhoAmI  
  4. App\Lib2\MyClass::WhoAmI  

PHP命名解析规则

PHP标识符名称使用下列命名空间规则进行解析,请参考PHP用户手册了解更详细的信息:

1.在编译时调用完全限定函数、类或常量;

2.非限定名称和限定名称根据导入规则进行翻译,例如,如果A\B\C导入为C,调用C\D\e( )就会被翻译成A\B\C\D\e( );

3.在PHP命名空间内,所有限定名称尚未根据导入规则转换,例如,如果在命名空间A\B中调用C\D\e( ),那么会被翻译成A\B\C\D\e( );

4.非限定类名称根据当前的导入规则进行转换,使用全名替换导入的短名称,例如,如果类C在命名空间A\B中被导入为X,那么new X( )就会被翻译为new A\B\C( );

5.在命名空间中非限定函数调用在运行时解析,例如,如果MyFunction( )在命名空间A\B中被调用,PHP首先会查找函数\A\B\MyFunction( ),如果没有找到,然后会在全局空间中查找\MyFunction( );

6.调用非限定或限定类名在运行时被解析,例如,如果我们在命名空间A\B中调用new C( ),PHP将会查找类A\B\C,如果没有找到,PHP会尝试自动载入A\B\C。


PHP命名空间高级特性

接下来让我们看一看PHP命名空间的一些高级特性。

__NAMESPACE__常量

__NAMESPACE__是一个PHP字符串,它总是返回当前命名空间的名称,在全局空间中它是一个空字符串。

<ol class="dp-c">
<li class="alt"><span><span></span></span></li>
<li><span>namespace App\Lib1;  </span></li>
<li class="alt">
<span class="func">echo</span><span> __NAMESPACE__; </span><span class="comment"><font color="#008200">// outputs: App\Lib1 </font></span><span> </span>
</li>
<li><span>?>  </span></li>
<li class="alt"><span> </span></li>
</ol>

这个值在调试时非常有用,它也可由于动态生成一个完全限定类名,如:

<ol class="dp-c">
<li class="alt"><span><span></span></span></li>
<li><span>namespace App\Lib1;  </span></li>
<li class="alt"><span> </span></li>
<li>
<span class="keyword"><strong><font color="#006699">class</font></strong></span><span> MyClass {  </span>
</li>
<li class="alt">
<span> </span><span class="keyword"><strong><font color="#006699">public</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> WhoAmI() {  </span>
</li>
<li>
<span class="keyword"><strong><font color="#006699">return</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">__METHOD__</font></strong></span><span>;  </span>
</li>
<li class="alt"><span> }  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li>
<span class="vars"><font color="#dd0000">$c</font></span><span> = __NAMESPACE__ . </span><span class="string"><font color="#0000ff">'\\MyClass'</font></span><span>;  </span>
</li>
<li class="alt">
<span class="vars"><font color="#dd0000">$m</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> </span><span class="vars"><font color="#dd0000">$c</font></span><span>;  </span>
</li>
<li>
<span class="func">echo</span><span> </span><span class="vars"><font color="#dd0000">$m</font></span><span>->WhoAmI(); </span><span class="comment"><font color="#008200">// outputs: App\Lib1\MyClass::WhoAmI </font></span><span> </span>
</li>
<li class="alt"><span>?>  </span></li>
</ol>

namespace关键字

namespace关键字可以用于明确引用一个当前命名空间或子命名空间中的项目,它等价于类中的self命名空间:

<ol class="dp-c">
<li class="alt"><span><span></span></span></li>
<li><span>namespace App\Lib1;  </span></li>
<li class="alt"><span> </span></li>
<li>
<span class="keyword"><strong><font color="#006699">class</font></strong></span><span> MyClass {  </span>
</li>
<li class="alt">
<span> </span><span class="keyword"><strong><font color="#006699">public</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> WhoAmI() {  </span>
</li>
<li>
<span class="keyword"><strong><font color="#006699">return</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">__METHOD__</font></strong></span><span>;  </span>
</li>
<li class="alt"><span> }  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li>
<span class="vars"><font color="#dd0000">$m</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> namespace\MyClass;  </span>
</li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="vars"><font color="#dd0000">$m</font></span><span>->WhoAmI(); </span><span class="comment"><font color="#008200">// outputs: App\Lib1\MyClass::WhoAmI </font></span><span> </span>
</li>
<li><span>?>  </span></li>
</ol>

自动载入命名空间类

PHP 5中最省时省力的特性是自动载入,在全局(非命名空间)PHP代码中,可以写一个标准自动载入函数:

<ol class="dp-c">
<li class="alt"><span><span></span></span></li>
<li>
<span class="vars"><font color="#dd0000">$obj</font></span><span>= </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> MyClass1(); </span><span class="comment"><font color="#008200">// classes/MyClass1.php is auto-loaded </font></span><span> </span>
</li>
<li class="alt">
<span class="vars"><font color="#dd0000">$obj</font></span><span>= </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> MyClass2(); </span><span class="comment"><font color="#008200">// classes/MyClass2.php is auto-loaded </font></span><span> </span>
</li>
<li><span> </span></li>
<li class="alt">
<span class="comment"><font color="#008200">// autoload function </font></span><span> </span>
</li>
<li>
<span class="keyword"><strong><font color="#006699">function</font></strong></span><span> __autoload(</span><span class="vars"><font color="#dd0000">$class_name</font></span><span>) {  </span>
</li>
<li class="alt">
<span> </span><span class="keyword"><strong><font color="#006699">require_once</font></strong></span><span>(</span><span class="string"><font color="#0000ff">"classes/$class_name.php"</font></span><span>);  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>?>  </span></li>
</ol>

在PHP 5.3中,你可以创建一个命名空间类的实例,在这种情况下,完全限定命名空间和类名传递给__autoload函数,例如,$class_name的值可 能是App\Lib1\MyClass。你可以在相同的文件夹下放置所有的PHP类文件,从字符串中提取命名空间,但那样会导致文件名冲突。

另外,你的类文件层次结构会按照命名空间的结构重新组织,例如,MyClass.php文件可以创建在/classes/App/Lib1文件夹下:

/classes/App/Lib1/MyClass.php

<ol class="dp-c">
<li class="alt"><span><span></span></span></li>
<li><span>namespace App\Lib1;  </span></li>
<li class="alt"><span> </span></li>
<li>
<span class="keyword"><strong><font color="#006699">class</font></strong></span><span> MyClass {  </span>
</li>
<li class="alt">
<span> </span><span class="keyword"><strong><font color="#006699">public</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> WhoAmI() {  </span>
</li>
<li>
<span class="keyword"><strong><font color="#006699">return</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">__METHOD__</font></strong></span><span>;  </span>
</li>
<li class="alt"><span> }  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>?>  </span></li>
</ol>

在根文件夹下的文件就使用下面的代码了:

myapp.php

<ol class="dp-c">
<li class="alt"><span><span></span></span></li>
<li>
<span class="keyword"><strong><font color="#006699">use</font></strong></span><span> App\Lib1\MyClass </span><span class="keyword"><strong><font color="#006699">as</font></strong></span><span> MC;  </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span class="vars"><font color="#dd0000">$obj</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> MC();  </span>
</li>
<li class="alt">
<span class="func">echo</span><span> </span><span class="vars"><font color="#dd0000">$obj</font></span><span>->WhoAmI();  </span>
</li>
<li><span> </span></li>
<li class="alt">
<span class="comment"><font color="#008200">// autoload function </font></span><span> </span>
</li>
<li>
<span class="keyword"><strong><font color="#006699">function</font></strong></span><span> __autoload(</span><span class="vars"><font color="#dd0000">$class</font></span><span>) {  </span>
</li>
<li class="alt">
<span> </span><span class="comment"><font color="#008200">// convert namespace to full file path </font></span><span> </span>
</li>
<li>
<span> </span><span class="vars"><font color="#dd0000">$class</font></span><span> = </span><span class="string"><font color="#0000ff">'classes/'</font></span><span> . </span><span class="func">str_replace</span><span>(</span><span class="string"><font color="#0000ff">'\\', '</font></span><span>/</span><span class="string"><font color="#0000ff">', $class) . '</font></span><span>.php';  </span>
</li>
<li class="alt">
<span> </span><span class="keyword"><strong><font color="#006699">require_once</font></strong></span><span>(</span><span class="vars"><font color="#dd0000">$class</font></span><span>);  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>?>  </span></li>
</ol>

解释:

1.类App\Lib1\MyClass的别名是MC;

2. new MC( )在编译时被翻译成new App\Lib1\MyClass( );

3.字符串App\Lib1\MyClass被传递给__autoload函数,使用文件路径正斜线替换所有命名空间中的反斜线,然后修改字符串,classes\App\Lib1\MyClass.php文件被自动载入;

总结

有关PHP命名空间的使用就介绍到这里,希望您能够对PHP的命名空间有一个新的认识,并希望你能在新项目中真正使用命名空间。

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment