PHP 5.3 的重要特性:命名空间
PHP 5.3 的一个新的重要特性就是 命名空间(namespace)。
这一特性在 PHP5.0x 时候就提出过,后来被取消并安排在 PHP6 中实现。而此次又再次“提前”到了 PHP 5.3 发布,可见开发人员对其的重视以及谨慎的态度。
官方发布时说明文档的内容可能已过期(documentation maybe out dated),所以在这里简单的说明命名空间的用法:首先是声明一个命名空间,加入了新的关键字 namespace ,其应在类文件的开头
1 2 3 4 5 6 7 8 Copy after login |
<span style="FONT-WEIGHT: bold; COLOR: #000000"><?php</span> namespace Project<span style="COLOR: #339933">::</span><span style="COLOR: #004000">Module</span><span style="COLOR: #339933">;</span> <span style="FONT-WEIGHT: bold; COLOR: #000000">class</span> User <span style="COLOR: #009900">{</span> <span style="FONT-WEIGHT: bold; COLOR: #000000">const</span> STATUS_OK <span style="COLOR: #339933">=</span> <span style="FONT-WEIGHT: bold; COLOR: #000000">true</span><span style="COLOR: #339933">;</span> <span style="FONT-WEIGHT: bold; COLOR: #000000">function</span> register<span style="COLOR: #009900">(</span><span style="COLOR: #000088">$data</span><span style="COLOR: #009900">)</span> <span style="COLOR: #009900">{</span> <span style="COLOR: #339933">...</span> <span style="COLOR: #009900">}</span> <span style="COLOR: #339933">...</span> <span style="COLOR: #009900">}</span> <span style="FONT-WEIGHT: bold; COLOR: #000000">?></span> Copy after login |
然后在控制器中(可能是其他文件)就可以这样调用
1 2 Copy after login Copy after login Copy after login |
<span style="COLOR: #000088">$user</span> <span style="COLOR: #339933">=</span> <span style="FONT-WEIGHT: bold; COLOR: #000000">new</span> Project<span style="COLOR: #339933">::</span><span style="COLOR: #004000">Module</span><span style="COLOR: #339933">::</span><span style="COLOR: #004000">User</span><span style="COLOR: #009900">(</span><span style="COLOR: #009900">)</span><span style="COLOR: #339933">;</span> <span style="COLOR: #000088">$user</span><span style="COLOR: #339933">-></span><span style="COLOR: #004000">register</span><span style="COLOR: #009900">(</span><span style="COLOR: #000088">$register_info</span><span style="COLOR: #009900">)</span><span style="COLOR: #339933">;</span> Copy after login |
的确与平常的并无两样,但是我们可以将两个相互独立的类联系起来。比如
1 2 Copy after login Copy after login Copy after login |
Project<span style="COLOR: #339933">::</span><span style="COLOR: #004000">Module</span><span style="COLOR: #339933">::</span><span style="COLOR: #004000">User</span><span style="COLOR: #339933">;</span> Project<span style="COLOR: #339933">::</span><span style="COLOR: #004000">Module</span><span style="COLOR: #339933">::</span><span style="COLOR: #004000">Blog</span><span style="COLOR: #339933">;</span> Copy after login |
这样就能从语言本身更容易描述和理解变量、类之间的关系,从而避免了“传统”上的 Project_Module_Blog 这样冗长的命名方式。
上面的说明可能很难说明使用命名空间带来了什么好处,新增加的 use 和 as 关键字或许能更好的说明问题。use 和 as 语句可以引用和声明 命名空间的“别名”。比如,上述的控制器中实例化类的代码可以这样写
1 2 3 Copy after login Copy after login Copy after login |
use Project<span style="COLOR: #339933">::</span><span style="COLOR: #004000">Module</span><span style="COLOR: #339933">;</span> <span style="COLOR: #000088">$user</span> <span style="COLOR: #339933">=</span> <span style="FONT-WEIGHT: bold; COLOR: #000000">new</span> Module<span style="COLOR: #339933">::</span><span style="COLOR: #004000">User</span><span style="COLOR: #009900">(</span><span style="COLOR: #009900">)</span><span style="COLOR: #339933">;</span> <span style="COLOR: #000088">$user</span><span style="COLOR: #339933">-></span><span style="COLOR: #004000">register</span><span style="COLOR: #009900">(</span><span style="COLOR: #000088">$register_info</span><span style="COLOR: #009900">)</span><span style="COLOR: #339933">;</span> Copy after login |
甚至
1 2 3 Copy after login Copy after login Copy after login |
use Project<span style="COLOR: #339933">::</span><span style="COLOR: #004000">Module</span><span style="COLOR: #339933">::</span><span style="COLOR: #004000">User</span> <span style="COLOR: #b1b100">as</span> ModuleUser<span style="COLOR: #339933">;</span> <span style="COLOR: #000088">$user</span> <span style="COLOR: #339933">=</span> <span style="FONT-WEIGHT: bold; COLOR: #000000">new</span> ModuleUser<span style="COLOR: #339933">;</span> <span style="COLOR: #000088">$user</span><span style="COLOR: #339933">-></span><span style="COLOR: #004000">register</span><span style="COLOR: #009900">(</span><span style="COLOR: #000088">$register_info</span><span style="COLOR: #009900">)</span><span style="COLOR: #339933">;</span> Copy after login |
类中的常量也可以通过命名空间访问,比如上述类中的 STATUS_OK 就可以通过命名空间
1 Copy after login Copy after login |
Project<span style="COLOR: #339933">::</span><span style="COLOR: #004000">Module</span><span style="COLOR: #339933">::</span><span style="COLOR: #004000">User</span><span style="COLOR: #339933">::</span><span style="COLOR: #004000">STATUS_OK</span> Copy after login |
访问。进一步的,也可以用别名简化那么长的“变量名称”
1 2 Copy after login Copy after login Copy after login |
use Project<span style="COLOR: #339933">::</span><span style="COLOR: #004000">Module</span><span style="COLOR: #339933">::</span><span style="COLOR: #004000">User</span><span style="COLOR: #339933">::</span><span style="COLOR: #004000">STATUS_OK</span> as STATUS_OK; echo STATUS_OK; Copy after login |
顺便提下“超空间(The Global Namespace)”的概念。所谓的“超空间”,就是没有指定命名空间的变量、类和函数。比如
1 2 3 Copy after login Copy after login Copy after login |
<span style="FONT-WEIGHT: bold; COLOR: #000000">function</span> foo<span style="COLOR: #009900">(</span><span style="COLOR: #009900">)</span> <span style="COLOR: #009900">{</span> <span style="COLOR: #339933">...</span> <span style="COLOR: #009900">}</span> Copy after login |
这的函数,可以使用 foo() 执行的同时,也可以使用 ::foo(); 这样执行。
最后,配合使用 autoload 函数即可载入指定命名空间的类。简单的函数如下
1 2 3 4 5 Copy after login |
<span style="FONT-WEIGHT: bold; COLOR: #000000">function</span> __autoload<span style="COLOR: #009900">(</span> <span style="COLOR: #000088">$classname</span> <span style="COLOR: #009900">)</span> <span style="COLOR: #009900">{</span> <span style="COLOR: #000088">$classname</span> <span style="COLOR: #339933">=</span> <span style="COLOR: #990000">strtolower</span><span style="COLOR: #009900">(</span> <span style="COLOR: #000088">$classname</span> <span style="COLOR: #009900">)</span><span style="COLOR: #339933">;</span> <span style="COLOR: #000088">$classname</span> <span style="COLOR: #339933">=</span> <span style="COLOR: #990000">str_replace</span><span style="COLOR: #009900">(</span> <span style="COLOR: #0000ff">'::'</span><span style="COLOR: #339933">,</span> DIRECTORY_SEPARATOR<span style="COLOR: #339933">,</span> <span style="COLOR: #000088">$classname</span> <span style="COLOR: #009900">)</span><span style="COLOR: #339933">;</span> <span style="COLOR: #b1b100">require_once</span><span style="COLOR: #009900">(</span> <span style="COLOR: #990000">dirname</span><span style="COLOR: #009900">(</span> <span style="FONT-WEIGHT: bold; COLOR: #000000">__FILE__</span> <span style="COLOR: #009900">)</span> <span style="COLOR: #339933">.</span> <span style="COLOR: #0000ff">'/'</span> <span style="COLOR: #339933">.</span> <span style="COLOR: #000088">$classname</span> <span style="COLOR: #339933">.</span> <span style="COLOR: #0000ff">'.class.php'</span> <span style="COLOR: #009900">)</span><span style="COLOR: #339933">;</span> <span style="COLOR: #009900">}</span> Copy after login |
这样,比如调用
1 Copy after login Copy after login |
__autoload<span style="COLOR: #009900">(</span><span style="COLOR: #0000ff">'Project::Module::User'</span><span style="COLOR: #009900">)</span><span style="COLOR: #339933">;</span> Copy after login |
就可以自动载入 Project_Module_User.class.php 文件(虽然这样看起来并不方便多少)。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

During the development process of Python, we often encounter module not found errors. The specific manifestation of this error is that Python reports one of two errors: ModuleNotFoundError or ImportError when importing the module. This error is very annoying and can cause the program to not run properly, so in this article, we will explore the causes of this error and how to solve it. ModuleNotFoundError and ImportError in Pyth

In the Java9 version, the Java language introduced a very important concept: module. If you are familiar with the modular management of JavaScript code, you should feel familiar when you see the modular management of Java 9. 1. What is Javamodule? Somewhat similar to packages in Java, modules introduce another level of grouping of Java code. Each such group (module) contains many sub-packages. Declare the folder and its subfolders as a module by adding the file module-info.java to the root of a module's source code file package. The file syntax

In Docker, the permission problem of the mounting directory can usually be solved by the following method: adding permission-related options when using the -v parameter to specify the mounting directory. You can specify the permissions of the mounted directory by adding: ro or :rw after the mounted directory, indicating read-only and read-write permissions respectively. For example: dockerrun-v/host/path:/container/path:roimage_name Define the USER directive in the Dockerfile to specify the user running in the container to ensure that operations inside the container comply with permission requirements. For example: FROMimage_name#CreateanewuserRUNuseradd-ms/bin/

1. First confirm the Linux system kernel [root@localhost~]#uname-r-p2.6.18-194.el5i6862. Go to http://sourceforge.net/projects/linux-ntfs/files/ to download the rpm package of the corresponding kernel. If you can't find the exact same one, you can find the closest one. I couldn't find the exact same one. What I downloaded is: kernel-module-ntfs-2.6.18-128.1.1.el5-2.1.27-0.rr.10.11.i686.rpm3. Install the rpm package rpm-ivhkernel -m

Vuex mainly consists of the following five parts: State, Getter, Mutation, Action, and Module. Below I will introduce Vuex Module-state warehouse segmentation. I hope it will be helpful to friends in need!

Python's os module is one of the standard libraries used for interacting with the operating system. It provides many useful functions and variables for working with files and directories. The following are the usage of some common os module functions: 1. Get the current working directory: importoscwd=os.getcwd()print(cwd) 2. Switch the current working directory: importosos.chdir('/path/to/new/directory' )3. List all files and subdirectories in the directory: importosfiles=os.listdir('/path/to/dire

Retro mechanical keyboards are all the rage these days, with the likes of the IBM Model M — although not technically mechanical — having gone from e-waste to veritable geek gold in recent years. However, the Model M isn't the only attractive keyboard

Retro mechanical keyboards are all the rage these days, with the likes of the IBM Model M — although not technically mechanical — having gone from e-waste to veritable geek gold in recent years. However, the Model M isn't the only attractive keyboard
