namespace(命名空间)相当于函数、类,划分了一个区域,这样子就使得在同个页面中可以require相同的类,使用相同名字的函数
: 在项目中比较少用
<code><span>//name.php</span><span><?php </span><span>//命名要使用复合名称</span><span>namespace</span><span>me</span>\<span>mine</span>; <span><span>class</span><span>me</span>{</span><span>public</span><span><span>function</span><span>__construct</span><span>()</span>{</span><span>echo</span><span>'name'</span>.<span>'<br>'</span>; } <span>public</span><span><span>function</span><span>name</span><span>()</span>{</span><span>echo</span><span>'i use space'</span>.<span>'<br>'</span>; } } <span>//$me = new me();</span><span><span>function</span><span>me</span><span>()</span>{</span><span>echo</span><span>'is me'</span>.<span>'<br>'</span>; }</span></code>
<code><span>//common.php</span><span><?php </span><span><span>class</span><span>me</span>{</span><span>public</span><span><span>function</span><span>__construct</span><span>()</span>{</span><span>echo</span><span>'no namespace'</span>.<span>'<br>'</span>; } }</span></code>
<code><span><span><?php </span><span>//建议使用别名</span><span>use</span><span>me</span>\<span>mine</span><span>as</span><span>i</span>; <span>require</span><span>'./name.php'</span>; <span>require</span><span>'./common.php'</span>; <span>$me</span> = <span>new</span> i\me(); <span>$me</span>->name(); i\me(); <span>$com</span> = <span>new</span> me();</span></span></code>
以上就介绍了PHP之namespace,包括了espace方面的内容,希望对PHP教程有兴趣的朋友有所帮助。