批改状态:未批改
老师批语:
get,set,call,callStatic魔术方法的实例

class Product{public $name = '鞋子';public $price = '99';public function __construct($name,$price){$this->name = $name;$this->price = $price;}}$p = new Product("帽子",33);echo '商品:'. $p->name.'<br>';echo '价格:'.$p->price.'元';
class Credit {private $idNum;private $age;public function __construct($idNum,$age){$this->idNum = $idNum;$this->age = $age;}public function __get($name){$method = 'get'.ucfirst($name);return method_exists($this,$method) ? $this->$method():null;}private function getIdNum(){return isset($this->idNum) ? '....'.mb_substr($this->idNum,2,3) : '身份证不合法';}private function getAge(){return isset($this->age) ? $this->age : '年龄不合法';}}$p = new Credit('202015198504054144',30);echo $p->idNum.'<br>';echo $p->age.'<br>';
class Credit{private $idNum;private $age;public function __construct($idNum,$age){$this->idNum = $idNum;$this->age = $age;}public function __set($name,$value){$method = 'set' . ucfirst($name);return method_exists($this,$method) ? $this->$method($value):null;}private function setIdNum($value){return $this->idNum = strlen($value) == 18 ? $value : null;}private function setAge($value){return $this->age = $value>0 && $value<200 ? $value :null;}}$p->idNum = '341215198504054148';echo $p->idNum.'<br>';$p->age = 30;echo $p->age;
class User{public function __call(string $name,array $args){printf('方法名:%s(),参数[%s]',$name,implode(',',$args));}public static function __callStatic(string $name,array $args){printf('方法名:%s(),参数[%s]',$name,implode(',',$args));}}$user = new User;$user->hello('z','f','g');echo '<hr>';User::demo(2011,2012,2013);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号