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

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Linux新版
SublimeText3 Linux最新版

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),