Blogger Information
Blog 17
fans 0
comment 0
visits 11602
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
命名空间
指纹指恋的博客
Original
554 people have browsed it

test_1.php

<?php
functon test(){
   echo "这里是test_1";
}

首先看一下文件目录结构

test_1.php
test_2.php
test_3.php

现在假设test_1.php和test_2.php,test_3.php三个文件内容如下

test_1.php

<?php
functon test(){
   echo "这里是test_1";
}
>

test_2.php

<?php
functon test(){
   echo "这里是test_2";
}
>

test_3.php

<?php
include("test_1.php");
include("test_1.php");
>

那么,现在运行test_3.php就会因为文件中有两个test ( ) 方法,造成函数重命名,而出现错误,而如果此时引入命名空间就可以很好的解决这个问题,代码修改如下;

test_1.php

<?php
namespace test_1;        //添加命名空间
functon test(){
   echo "这里是test_1";
}
>

test_2.php

<?php
namespace test_2;        ////添加命名空间
functon test(){
   echo "这里是test_2";
}
>

命名空间通俗的解释就是,有多个同名的孩子(方法),如果这些孩子中有两个或两个以上同时来到一个家庭(PHP文件),那么直接喊(调用)名字(方法)就会发生冲突,不知道具体要叫谁,假设这些孩子都叫小明,现在,对他们进行区分,分别定义为老张家的小明,老李家的小明,老王家的小明,等等,这样再叫的时候就不会发生错误了,如下图所示:

FCB22C7F-4045-49AE-9729-45FE423B2785.png

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!