<?php class car { //属性 private $brand; private $type; private $color; private $size=[]; //构造方法 public function __construct($brand='',$type='',$color='',array $size=[]) { $this->brand = $brand; $this->type = $type; $this->color = $color; $this->size = $size; } //__get魔术方法 查询器 public function __get($property_name) { $msg = null; if (isset($this->$property_name)){ $msg = $this->$property_name; } else { $msg = '属性'.$property_name.'不存在!'; } return $msg; } //设置器 public function __set($property_name,$value) { $this->$property_name = $value; } } ?>
点击 "运行实例" 按钮查看在线实例
<?php require './class/car.php'; $newcar = new car('宝马','X5','白色',[4909,1938,1772]); // get测试 echo '品牌:'.$newcar->brand.'<br>'; echo '型号:'.$newcar->type.'<br>'; echo '颜色:'.$newcar->color.'<br>'; echo '尺寸: '. print_r($newcar->size,true). '<br>'; echo 'GPS:'.$newcar->gps.'<br>'; echo '<hr>'; // set测试 $newcar->brand = '奥迪'; $newcar->type = 'A6'; $newcar->color = '黑色'; $newcar->size = [5012,1855,1485]; echo '品牌:'.$newcar->brand.'<br>'; echo '型号:'.$newcar->type.'<br>'; echo '颜色:'.$newcar->color.'<br>'; echo '尺寸: '. print_r($newcar->size,true). '<br>'; ?>
点击 "运行实例" 按钮查看在线实例