-------------------------------------------------------------------------------------------------- 一.PHP的命名空间主要解决三种冲突的问题:常量,函数,类 通俗理解:namespace就相当于是用来建一个目录,将namespace下面的代码放在该目录里面,与
--------------------------------------------------------------------------------------------------
一. PHP的命名空间主要解决三种冲突的问题:常量,函数,类
通俗理解:namespace就相当于是用来建一个目录,将namespace下面的代码放在该目录里面,与外面的区分开来。
<span>/*</span><span> |--------------------------------- |namespace示例 |@黑眼诗人 <www.farwish.com> |--------------------------------- </www.farwish.com></span><span>*/</span><span> namespace myself; </span><span>function</span> <span>var_dump</span><span>() { </span><span>echo</span> 100<span>; } </span><span>var_dump</span>(); <span>//</span><span>调用自定义函数(相对路径方式)</span> <span> \myself\</span><span>var_dump</span>(); <span>//</span><span>调用自定义函数(绝对路径方式) </span> <span> \</span><span>var_dump</span>(100); <span>//</span><span>调用全局的(系统的函数)</span>
注意:namespace 之前不能有任何代码,除了declare();多个文件可以使用同一个命名空间,但同一命名空间下定义的内容是不能冲突的。namespace支持子命名空间,如:namespace \myself\good,相当于多级目录的概念。
二. 同一文件中存在多个命名空间的情况
1.
<span>/**<br> * <span>同一文件中若使用了多个命名空间,一般写法</span><br> */<br>namespace nihao\shijie;<br> function demo()<br>{<br> //.......<br>}<br> namespace hello\world; </span><span>function</span><span> test() { </span><span>//</span><span>........</span> <span>} </span><span><br>\nihao\shijie\demo();<br>\hello\world\test();<br></span>
2.
<span>/*</span><span>* * 同一文件中若使用了多个命名空间,建议大括号扩起来 </span><span>*/</span><span> namespace nihao\shijie{ </span><span>function</span><span> test_one()<br> { </span><span>//</span><span>......</span> <span> }; } namespace hello\world{ </span><span>function</span><span> test_two()<br> { </span><span>//</span><span>........</span> <span> } } \nihao\shijie\test_one(); \hello\world\test_two();</span>
同一文件内使用多个命名空间,主要用于项目将多个PHP脚本合并在同一文件中,实际中不提倡使用!
三. 名称解析规则(几个概念)
1.非限定名称:名称中不包含命名空间分割符,如:myself
2.限定名称:名称中含有命名空间分割符,如:nihao\shijie
3.完全限定名称:名称中包含分割符,并以命名空间分割符开始,如:\nihao\shijie (即绝对路径的概念)
Link: http://www.cnblogs.com/farwish/p/3913663.html
@黑眼诗人

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

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.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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