在项目目录下创建test文件,创建namespace.php和namespaceB.php两个文件
内容均为
<?php
class Apple{
function get_info(){
echo 'this is a';
}
}
在创建index.php文件引入两个文件
<?php
require_once('namespace.php');
require_once('namespaceB.php');
会报错
命名空间
在namespace文件
<?php
namespace a\b\c;
class Apple{
function get_info(){
echo 'this is a';
}
}
namespaceB
<?php
namespace d\e\f;
class Apple{
function get_info(){
echo 'this is b';
}
}
在index.php 当中引用的时候
$a_app = new a\b\c\Apple();
为了方便,在多次引用的时候
use a\b\c\Apple();
两个都要运用的时候,要做区分
use a\b\c\Apple() as A;起一个别名
对于没有在命名空间的称为全局类
使用的时候 new \Apple();