Correction status:qualified
Teacher's comments:
1.创建一个类文件Programmer.php
<?php class Programmer { private $name = ''; private $age = 0; private $language = ''; public function __construct($name='',$age=0,$language='') { $this->name = $name; $this->age = $age; $this->language = $language; } public function __get($name) { $msg = null; if(isset($this->$name)) { $msg = $this->$name; }else{ $msg = '无此属性'; } return $msg; } public function __set($name,$value) { $this->$name = $value; } }
2.创建一个php脚本进行调用
<?php include 'class/Programmer.php'; $programmer1 = new Programmer('大神',24,'php'); //测试查询器__get() echo '姓名:'.$programmer1->name .'<br>'; echo '年龄:'.$programmer1->age .'<br>'; echo '编程语言:'.$programmer1->language .'<br>'; echo '<hr>'; //测试设置器__set() $programmer1->name = '肖奈'; $programmer1->age = 26; $programmer1->language ='python'; echo '姓名:'.$programmer1->name .'<br>'; echo '年龄:'.$programmer1->age .'<br>'; echo '编程语言:'.$programmer1->language .'<br>';
运行结果: