批改状态:合格
老师批语:

<?phpclass Demo{protected $data = ['username' => 'guest'];//属性重载public function __get($name){$method = 'get' . ucfirst($name);return $this->$method($name);}// 获取器public function getUsername($name){if (!isset($this->data[$name])) {$this->data[$name] = 0;} else {return $this->data[$name];}}public function getEmail($name){if(!isset($this->data[$name])) {$this->data[$name] = 0;} else {return $this->data[$name];}}public function __set($name, $value){$method = 'set' . ucfirst($name);return $this->$method($name,$value);}// 设置器public function setEmail($name,$value){$this->data[$name] = $value;}// 方法重载// 普通方法:__call()// 静态方法:__callStatic()public function __call($name,$args){// 请求转发return $name . '[' . implode(',',$args) . ']';}// 当访问一个不存在的静态方法,会定位到下面这个魔术方法中public static function __callStatic($name, $args){return $name . '[' . implode(',',$args) . ']';}}$obj = new Demo;echo $obj->username,'<br>';$obj->email = 'guest@qq.com';echo $obj->email;echo '<br>';echo $obj->world(1,2,3);echo '<br>';echo Demo::test('a','b','b');
全局成员:类,常量,函数
特点:不能重复声明

<?php// 类class Mod{public $mod = __CLASS__;}// 常量const TITLE = 'Hello';// 函数function con(){return __FUNCTION__;}echo (new Mod)->mod,'<br>';echo TITLE,'<br>';echo con();

<?php// 命名空间namespace ns1 {// 类class Mod1{}// 常量const TITLE = '标题';// 函数function test(){}echo Mod1::class, '<br>';echo TITLE::class, '<br>';echo test::class, '<br>';}// 全局空间namespace {echo '<hr>';// 类class Mod1{}// 常量const TITLE = '标题';// 函数function test(){}echo Mod1::class, '<br>';echo TITLE::class, '<br>';echo test::class, '<br>';}

<?phpnamespace ns;require 'test1.php';require 'test2.php';echo Test1::class,'<br>';echo Test2::class,'<br>';
<?phpnamespace ns;class Test1{public $username = 'guest';}
<?phpnamespace ns;class Test2{public $title = 'php中文网';}

<?php// 父空间namespace test1 {class Demo{}echo Demo::class,'<br>';// namespance:用在空间中,表示当前空间的引用,类似于$this或selfecho test2\Demo::class,'<br>';echo test2\test3\Demo::class,'<hr>';}// test1的子空间namespace test1\test2 {class Demo{//...}echo Demo::class,'<br>';echo test3\Demo::class,'<hr>';}// test2的子空间namespace test1\test2\test3 {class Demo{//...}echo Demo::class,'<br>';// 在test3中访问上级空间echo \test1\test2\Demo::class,'<br>';echo \test1\Demo::class,'<br>';// 全局成员:前面加上全局空间的标识符:\echo \Demo::class,'<br>';}// 全局空间namespace {class Demo{}}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号