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>; }</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>; } }</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>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介紹了PHP之namespace,包含了espace方面的內容,希望對PHP教學有興趣的朋友有幫助。