function __construct($name="unnamed")
{
$this->name = $name;
}
}
class House
{
//array of rooms
public $room;
}
//create empty house
$home = new house;
//add some rooms
$home->room[] = new Room("bedroom");
$home->room[] = new Room("kitchen");
$home->room[] = new Room("bathroom");
//show the first room of the house
print($home->room[0]->name);
?>
PHP有两个特别的定名空间:parent定名空间指向父类,self定名空间指向以后的类。上面的例子中显示了若何用parent定名空间来挪用父类中的机关函数. 同时也用self来在机关函数中挪用另外一个类办法。
class Animal //植物
{
public $blood; //热血or冷血属性
public $name;
public function __construct($blood, $name=NULL)
{
$this->blood = $blood;
if($name)
{
$this->name = $name;
}
}
}
class Mammal extends Animal //哺乳植物
{
public $furColor; //外相色彩
public $legs;