<?php
namespace Test1;
const SITE_NAME='PHP中文网';
function sum($m,$n){
return $m+$n;
}
class staff{
private $name='Peter';
public function __get($name){
return $this->$name;
}
public function __set($name,$value){
return $this->$name=$value;
}
}
namespace Test2;
// use Test1;//只定位到空间,并不定位到类
use Test1\staff as test1Staff;//定位到空间中的类
use Test2\Test3\Demo;//引用子空间下的类
const SITE_NAME='www.php.cn';
function sum($m,$n){
return $m+$n;
}
class staff{
private $name='Jack';
public function __get($name){
return $this->$name;
}
public function __set($name,$value){
return $this->$name=$value;
}
}
echo '当前的命名空间是:'.__namespace__;
echo '<hr>';
echo SITE_NAME;//不加内容,为非限定名称的命名空间
echo '<hr>';
echo \Test1\SITE_NAME;//完全限定的命名空间
echo '<hr>';
echo sum(20,30);
echo '<hr>';
$obj = new staff();
echo $obj->name;
echo '<hr>';
$obj1 = new test1Staff;//访问当前命名空间的其它空间的类(44行)
$obj1->name = 'Tom';
echo $obj1->name;
echo '<hr>';
echo Demo::CITY;
namespace Test2\Test3;
class Demo{
const CITY='合肥';
}
?>