实例
<?php /** * 1.对象的创建 * 2.对象成员的初始化 * 3.对象成员的访问 */ class Person{ public $name; public $age; private $salary; private $weight; function __construct($name,$age,$salary,$weight) { $this->name = empty($name) ? '' : $name; $this->age = empty($age) ? '没有填写年龄' : $age; $this->salary = $salary; $this->weight = $weight; } function __get($name) { $msg='无此属性'; if(!empty($this->$name)) { $msg=$this->$name; } return $msg; } function __set($name,$value) { $this->$name = $value; } } $person = new Person('Tom',22,3000,''); echo '姓名:'.$person->name,'<br>'; echo '年龄:'.$person->age,'<br>'; echo '体重:'.$person->weight,'<br>'; echo '工资:'.$person->salary;
运行实例 »
点击 "运行实例" 按钮查看在线实例