Blogger Information
Blog 10
fans 0
comment 1
visits 8276
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
命名空间
夏涛的博客
Original
687 people have browsed it

<?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='合肥';

}


 ?>



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
  • 1970-01-01 08:00:00