PHP 5.3 特性:命名空间_PHP教程
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 文件

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

多次调用session_start()会导致警告信息和可能的数据覆盖。1)PHP会发出警告,提示session已启动。2)可能导致session数据意外覆盖。3)使用session_status()检查session状态,避免重复调用。

AI可以帮助优化Composer的使用,具体方法包括:1.依赖管理优化:AI分析依赖关系,建议最佳版本组合,减少冲突。2.自动化代码生成:AI生成符合最佳实践的composer.json文件。3.代码质量提升:AI检测潜在问题,提供优化建议,提高代码质量。这些方法通过机器学习和自然语言处理技术实现,帮助开发者提高效率和代码质量。

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

MySQL函数可用于数据处理和计算。1.基本用法包括字符串处理、日期计算和数学运算。2.高级用法涉及结合多个函数实现复杂操作。3.性能优化需避免在WHERE子句中使用函数,并使用GROUPBY和临时表。

HTML5带来了五个关键改进:1.语义化标签提升了代码清晰度和SEO效果;2.多媒体支持简化了视频和音频嵌入;3.表单增强简化了验证;4.离线与本地存储提高了用户体验;5.画布与图形功能增强了网页的可视化效果。

Composer是PHP的依赖管理工具,通过composer.json文件管理项目依赖。1)解析composer.json获取依赖信息;2)解析依赖关系形成依赖树;3)从Packagist下载并安装依赖到vendor目录;4)生成composer.lock文件锁定依赖版本,确保团队一致性和项目可维护性。

在MySQL中配置字符集和排序规则的方法包括:1.设置服务器级别的字符集和排序规则:SETNAMES'utf8';SETCHARACTERSETutf8;SETCOLLATION_CONNECTION='utf8_general_ci';2.创建使用特定字符集和排序规则的数据库:CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci;3.创建表时指定字符集和排序规则:CREATETABLEexample_table(idINT

typetraits在C 中用于编译时类型检查和操作,提升代码的灵活性和类型安全性。1)通过std::is_integral和std::is_floating_point等进行类型判断,实现高效的类型检查和输出。2)使用std::is_trivially_copyable优化vector拷贝,根据类型选择不同的拷贝策略。3)注意编译时决策、类型安全、性能优化和代码复杂性,合理使用typetraits可以大大提升代码质量。
