批改状态:合格
老师批语:
class text{//属性private $data = ['name' => '新手1314'];//属性重载public function __get($user){//array_key_exists:检查数组里是否有指定的键名或索引if(array_key_exists($user,$this->data)){return $this ->data[$user];}return "属性{$user}不存在";}}$text = new text;echo $text ->name;//输出结果:新手1314
class text1{private $data = ['age' => 14];public function __get($user){if(array_key_exists($name,$this->data)){reutrn $this ->data[$user];}return "属性{$user}不存在";}//更新拦截器(动态)public function __set($name,$value){if(array_key_exists($name,$this ->data)){if($name === 'age'){if($value >= 18 && age <= 60){return $this -> data[$user] = $value;}else{echo '年龄必须在18-60之间';}}else{echo $this ->data[$user] = $value;}}else{echo '禁止动态创建属性';}}}$text1 =new text1;//动态创建属性$text1 ->age =4;echo $text1 ->age;//输出打印结果:年龄必须在18-60之间14
class text2{//方法拦截器public function __call($name,$args){printf('方法:%s<br><pre>%s</pre>',$name,print_r($args,true));}}$text2 =new text2;$text2 -> hello('新手1314','php.cn');//输出结果:方法:hello// 参数:// Array// (// [0] => 新手1314// [1] => php.cn// )
class text3{//静态方法拦截器public static function __callStatic($name,$args){printf('方法:%s<br><pre>%s</pre>',$name,print_r($args,true));}}$text3 =new text3;text3::money(100,300);//输出结果:方法:money// Array// (// [0] => 100// [1] => 300// )
namespace A{const name = '新手1314';}//命名空间的子空间namespace B\C{const name = 'php中文网';}namespace B{const name = '新手';echo name . '<br>';echo \A\name . '<br>';echo C\name . '<br>';}//全局空间,可访问当前所有的空间namespace{echo '子空间A的输出:'. \A\name.'<br>';echo '子空间B的输出:'. \B\name.'<br>';echo '子空间C的输出:'. \B\C\name;}//输出结果为:新手// 新手1314// php中文网// 子空间A的输出:新手1314// 子空间B的输出:新手// 子空间C的输出:php中文网
//spl_autoload_register:注册给定的函数作为 __autoload 的实现sql_autoload_register(function ($class){//str_replace:子字符串替换$path = str_replace('\\', DIRECTORY_SEPARATOR,$class);$file = __DIR__ .DIRECTORY_SEPARATOR.$path.'.php ';require $file;})new \php\cn\Zuoye;//输出结果:php\cn\Zuoye
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号