文章利用舉例說明了關於PHP5面向對象訪問控制 Public,private,protected詳細說明,有需要的朋友可以參考一下。
在PHP5中增強了面向對象的機制,加入了面向對象常見的public、private、protected這樣的訪問控制機制。從字面意思來理解:
Public 無疑是公共的意思,意思是說類本身和其外部的子類都可以訪問這個屬性或者方法;
Private 英文翻譯過來就是私有的意思,只能是類本身在類的內部才能訪問——實例化的對象句柄不能訪問該屬性和方法,子類也不能訪問;
Protected 受保護的屬性或者方法,這個屬性或者是方法只能被類本身或者子類的內部訪問,實例化的對象句柄不能訪問。
現在我們舉個例子來說這個問題。
假設有一個寶石博物館的管理員 小李,把倉庫中的寶石分為三個類別,紅寶石,藍寶石,綠寶石。並且劃分了屬性,紅寶石是國家的任何人都可以參觀標簽為(public),藍寶石管理員小李家族傳下來的(protected),綠寶石是小李自己在山上見到的(private)。
那麼我們可以這樣認為:
public標簽的紅寶石,是國家的——全民所有,只要是合法的國家的公民都能夠參看和拍照。
Protected 這個是受保護的,只有小李家族的人(小李的孩子或者孫子)在特定的房間裡面才能參觀和拍照,不准拿到房間外邊看——太小氣了!
Private 這個是私有的,小李不想讓其他人知道,更不想讓拍照了,所以只能供小李在小李的房間裡看看而已,其他的人即使小李的孩子們都沒有辦法看到
實例
<?php
error_reporting(E_ALL);
class test{
public $public;
private $private;
protected $protected;
static $instance;
public function __construct(){
$this->public = 'public <br>';
$this->private = 'private <br>';
$this->protected = 'protected <br>';
}
static function tank(){
if (!isset(self::$instance[get_class()]))
{
$c = get_class();
self::$instance = new $c;
}
return self::$instance;
}
public function pub_function() {
echo "you request public function<br>";
echo $this->public;
echo $this->private; //private,內部可以調用
echo $this->protected; //protected,內部可以調用
$this->pri_function(); //private方法,內部可以調用
$this->pro_function(); //protected方法,內部可以調用
}
protected function pro_function(){
echo "you request protected function<br>";
}
private function pri_function(){
echo "you request private function<br>";
}
}
$test = test::tank();
echo $test->public;
echo $test->private; //Fatal error: Cannot access private property test::$private
echo $test->protected; //Fatal error: Cannot access protected property test::$protected
$test->pub_function();
$test->pro_function(); //Fatal error: Call to protected method test::pro_function() from context
$test->pri_function(); //Fatal error: Call to private method test::pri_function() from context
?>
<?php error_reporting(E_ALL); class test{ public $public; private $private; protected $protected; static $instance; public function __construct(){ $this->public = 'public <br>'; $this->private = 'private <br>'; $this->protected = 'protected <br>'; } static function tank(){ if (!isset(self::$instance[get_class()])) { $c = get_class(); self::$instance = new $c; } return self::$instance; } public function pub_function() { echo "you request public function<br>"; echo $this->public; echo $this->private; //private,內部可以調用 echo $this->protected; //protected,內部可以調用 $this->pri_function(); //private方法,內部可以調用 $this->pro_function(); //protected方法,內部可以調用 } protected function pro_function(){ echo "you request protected function<br>"; } private function pri_function(){ echo "you request private function<br>"; } } $test = test::tank(); echo $test->public; echo $test->private; //Fatal error: Cannot access private property test::$private echo $test->protected; //Fatal error: Cannot access protected property test::$protected $test->pub_function(); $test->pro_function(); //Fatal error: Call to protected method test::pro_function() from context $test->pri_function(); //Fatal error: Call to private method test::pri_function() from context ?>
從上面的例子中,我們可以看出,
public: 可以class內部調用,可以實例化調用。
private: 可以class內部調用,實例化調用報錯。
protected: 可以class內部調用,實例化調用報錯。
<?php
class test{
public $public;
private $private;
protected $protected;
static $instance;
public function __construct(){
$this->public = 'public <br>';
$this->private = 'private <br>';
$this->protected = 'protected <br>';
}
protected function tank(){ //私有方法不能繼承,換成public,protected
if (!isset(self::$instance[get_class()]))
{
$c = get_class();
self::$instance = new $c;
}
return self::$instance;
}
public function pub_function() {
echo "you request public function<br>";
echo $this->public;
}
protected function pro_function(){
echo "you request protected function<br>";
echo $this->protected;
}
private function pri_function(){
echo "you request private function<br>";
echo $this->private;
}
}
class test1 extends test{
public function __construct(){
parent::tank();
parent::__construct();
}
public function tank(){
echo $this->public;
echo $this->private; //Notice: Undefined property: test1::$private
echo $this->protected;
$this->pub_function();
$this->pro_function();
$this->pri_function(); //Fatal error: Call to private method test::pri_function() from context 'test1'
}
public function pro_extends_function(){
echo "you request extends_protected function<br>";
}
public function pri_extends_function(){
echo "you request extends_private function<br>";
}
}
error_reporting(E_ALL);
$test = new test1();
$test -> tank(); //子類和父類有相同名字的屬性和方法,實例化子類時,子類的中的屬性和方法會蓋掉父類的。
?>