Correction status:qualified
Teacher's comments:
创建命名空间和访问其中内容:
<?php namespace space1{ class person{ public $name,$age; function __construct($name='space1小明',$age='15'){ $this->name = $name; $this->age = $age; } public function show(){ echo $this->name,"今年",$this->age,"岁<br>"; } } } namespace space2{ class person{ public $name,$age; function __construct($name='space2小红',$age='120'){ $this->name = $name; $this->age = $age; } public function show(){ echo $this->name,"今年",$this->age,"岁<br>"; } } } namespace{ class person{ public $name,$age; function __construct($name='本空间小刚',$age='11'){ $this->name = $name; $this->age = $age; } public function show(){ echo $this->name,"今年",$this->age,"岁<br>"; } } //调用命名空间space1中的person类 $p = new \space2\person(); $p->show(); //调用命名空间space2中的person类 $p = new \space1\person(); $p->show(); //调用本空间中的person类 $p = new person(); $p->show(); } ?>
点击 "运行实例" 按钮查看在线实例
2.使用别名访问,引入其他目录下的脚本:
<?php require "A/B/C/c1.php"; require "A/B/C/c2.php"; use A\B\C\c1\space2 as pc2; use A\B\C\c1\space1; class person{ public $name,$age; function __construct($name='本空间小刚',$age='11'){ $this->name = $name; $this->age = $age; } public function show(){ echo $this->name,"今年",$this->age,"岁<br>"; } } //调用命名空间space1中的person类 $p = new space1\person1(); $p->show(); //调用命名空间space2中的person类 $p = new pc2\person(); $p->show(); //调用本空间中的person类 $p = new person(); $p->show(); ?>
点击 "运行实例" 按钮查看在线实例
3.非限定名称=》当前文件;限定名称=》相对路径;完全限定名称=》绝对路径;
非限定名称:new space1(); 如果当前命名空间是 A\B 则这个实例化的是A\B\space1
限定名称:new C\space1(); 如果当前命名空间是A\B 则这个实例化的是A\B\C\space1
完全限定名称:new \C\space1();如果当前命名空间是A\B,则这个实例化的是 C\space1