批改状态:合格
老师批语:
<?php/*** 1.属性重载:__get(),__set();* 2.方法重载:__call(),__callStatic();*/class User{private $data = ['name' => '张老师','age' =>null,];//查询拦截器public function __get($name){if(array_key_exists($name,$this->data)){return $this->data[$name];}return "属性{$name}不存在";}//更新拦截器public function __set($name,$value){//是否有键值if(array_key_exists($name,$this->data)){//是否合法if($name === 'age'){if($value>=18 && $value<=59){return $this->data[$name] = $value;}else{echo '年龄必须在18-59之间';}}else{return $this->data[$name] = $value;}}else{echo '禁止动态创建属性';}}//方法拦截器public function __call($name,$args){printf('方法:%s<br><pre> 参数:%s</pre>',$name,print_r($args,true));}//方静态法拦截器public static function __callStatic($name,$args){printf('方法:%s<br><pre> 参数:%s</pre>',$name,print_r($args,true));}}$user = new User;echo $user->name.'<br>';$user->age = 30;echo $user->age.'<br>';echo '<hr color=red>';$user->hello('name','张老师');User::word(1000,20000);
<?php/*** 命名空间使用namespace关键字声明,必须写到第一行* 一个脚本可以创建多个命名空间**/namespace ns1{//空间成员const APP = '商城';}//空间分级管理:子空间namespace ns2\ns3{const APP = '问答';echo APP . '<br>';}namespace ns2{//空间成员const APP = '社区';class Demo{}function index(){}//非限定名称:从当前空间查询echo APP . '<br>';//完全限定名称:从根空间开始查询echo \ns1\APP . '<br>';//限定名称:子空间 ns3\APP;echo ns3\APP;}
<?phpnamespace ns1;//注册一个类的自动加载器/*** 1.一个文件一个命名空间一个类* 2.命名空间的名与成员路径一致* 3.类名与文件名一致*/spl_autoload_register(function($class){$path = str_replace('\\',DIRECTORY_SEPARATOR,$class);echo $path;$file = __DIR__ . DIRECTORY_SEPARATOR . $path . '.php';require $file;});new \php\Demo;
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号