Correction status:qualified
Teacher's comments:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/3 * Time: 15:17 */ class conn { //声明类属性 // public $name= '王先生'; // // public $city = '鞍山'; // // public $like =['吃','喝','玩','乐']; public $name =''; public $city =''; public $like =[]; // 构造方法,对象属性的初始化 public function __construct($name ,$city ,array $like) { $this->name = $name; $this->city = $city; $this->like = $like; } //查询器 __get() public function __get($city) { // TODO: Implement __get() method. return $this->$city; } //设置器 __set() public function __set($city, $value) { // TODO: Implement __set() method. if (isset($this->$city)) { $this->$city =$value; } else { $this->data[$name] = $value; } } }
点击 "运行实例" 按钮查看在线实例
调用类
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/3 * Time: 15:34 */ //导入类 require './class/conn.php'; //创建类的实例 //$conn =new conn(); //测试 $conn =new conn('王','鞍',['吃','喝','玩','乐']); //var_dump($conn); $conn->city ='山'; echo $conn->city;
点击 "运行实例" 按钮查看在线实例
总结: 类是对象的模板,对象是类的实例