第九節--綁定
除了限制訪問,訪問方式也決定哪個方法將被子類調用或哪個屬性將被子類訪問. 函數調用與函數本身的關聯,以及成員訪問與變量內存地址間的關系,稱為綁定.
在計算機語言中有兩種主要的綁定方式—靜態綁定和動態綁定. 靜態綁定發生於數據結構和數據結構間,程序執行之前. 靜態綁定發生於編譯期, 因此不能利用任何運行期的信息. 它針對函數調用與函數的主體,或變量與內存中的區塊. 因為PHP是一種動態語言,它不使用靜態綁定. 但是可以模擬靜態綁定.
動態綁定則針對運行期產生的訪問請求,只用到運行期的可用信息. 在面向對象的代碼中,動態綁定意味著決定哪個方法被調用或哪個屬性被訪問,將基於這個類本身而不基於訪問范圍.
Public和protected成員的動作類似於PHP的前幾個版本中函數的動作,使用動態綁定. 這意味著如果一個方法訪問一個在子類中被覆寫的類成員,並是一個子類的實例,子類的成員將被訪問(而不是訪問父類中的成員).
看例子6.10. 這段代碼輸出” Hey! I am Son.” 因為當PHP調用getSalutation, 是一個Son的實例,是將Father中的salutation覆寫而來. 如果salutation是public的,PHP將產生相同的結果. 覆寫方法的操作很類似.在Son中,對於identify的調用綁定到那個方法.
即使在子類中訪問方式被從protected削弱成public, 動態綁定仍然會發生. 按照訪問方式使用的原則,增強對於類成員的訪問限制是不可能的. 所以把訪問方式從public改變成protected不可能進行.
Listing 6.10 Dynamic binding 動態綁定
<?php
class Father
{
protected $salutation = "Hello there!"; //問候
public function getSalutation()
{
print("$this->salutation
");
$this->identify();
}
protected function identify()
{
print("I am Father.<br>
");
}
};
class Son extends Father
{
protected $salutation = "Hey!"; //父類中的protected $salutation 被覆寫
protected function identify() //父類中的protected identify() 被覆寫
{
print("I am Son.<br>
");
}
};
$obj = new Son();
$obj->getSalutation(); //輸出Hey! I am Son.
?> <?php
class Father
{
private $salutation = "Hello there!";
public function getSalutation()
{
print("$this->salutation
");
$this->identify();
}
private function identify()
{
print("I am Father.<br>
");
}
}
class Son extends Father
{
private $salutation = "Hey!";
private function identify()
{
print("I am Son.<br>
");
}
}
$obj = new Son();
$obj->getSalutation(); //輸出Hello there! I am Father.
?><?php
class User //用戶
{
protected function isAuthorized() //是否是驗證用戶
{
return(FALSE);
}
public function getName() //獲得名字
{
return($this->name);
}
public function deleteUser($username) //刪除用戶
{
if(!$this->isAuthorized())
{
print("You are not authorized.<br>
");
return(FALSE);
}
//delete the user
print("User deleted.<br>
");
}
}
class AuthorizedUser extends User //認證用戶
{
protected function isAuthorized() //覆寫isAuthorized()
{
return(TRUE);
}
}
$user = new User;
$admin = new AuthorizedUser;
//not authorized
$user->deleteUser("Zeev");
//authorized
$admin->deleteUser("Zeev");
?>