看例子6.10. 这段代码输入” Hey! I am Son.” 由于当PHP挪用getSalutation, 是一个Son的实例,是将Father中的salutation覆写而来. 假如salutation是public的,PHP将发生不异的了局. 覆写办法的操作很相似.在Son中,关于identify的挪用绑定到谁人办法.
Private成员只存在于它们地点的类外部. 不像public和protected成员那样,PHP摹拟静态绑定. 看例子6.11. 它输入”Hello there! I am Father.”,虽然子类覆写了salutation的值. 剧本将this->salutation和以后类Father绑定. 相似的准绳使用于private办法identify().
Listing 6.11 Binding and private members
<?php
class Father
{
private $salutation = "Hello there!";
public function getSalutation()
{
print("$this->salutationn");
$this->identify();
}
private function identify()
{
print("I am Father.<br>n");
}
}
class Son extends Father
{
private $salutation = "Hey!";
private function identify()
{
print("I am Son.<br>n");
}
}
$obj = new Son();
$obj->getSalutation(); //输入Hello there! I am Father.
?>