php类的属性设置和访问
/class/Animal.php
实例
<?php /** * */ class Animal { private $height; private $weight; private $age; public function __construct($height,$weight,$age) { $this->height=$height; $this->weight=$weight; $this->age=$age; } public function __get($name) { if (isset($this->$name)){ echo "Getting ".$name.':'.$this->$name.'<br>'; } else { echo '没有初始值'; } } public function __set($name, $value) { if (isset($this->$name)) { $this->$name=$value; } else { echo $name.'not set'; } } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * */ require './class/Animal.php'; $animal=new Animal(110,70,5); $animal->height; $animal->weight; $animal->age; echo '<hr>'; $animal->height=120; $animal->weight=80; $animal->age=7; $animal->height; $animal->weight; $animal->age;
运行实例 »
点击 "运行实例" 按钮查看在线实例