php的public、protected、private三種訪問控制模式的區別,protectedprivate
php的public、protected、private三種訪問控制模式的區別
public: 公有類型
在子類中可以通過self::var調用public方法或屬性,parent::method調用父類方法
在實例中可以能過$obj->var 來調用 public類型的方法或屬性
protected: 受保護類型
在子類中可以通過self::var調用protected方法或屬性,parent::method調用父類方法
在實例中不能通過$obj->var 來調用 protected類型的方法或屬性
private: 私有類型
該類型的屬性或方法只能在該類中使用,在該類的實例、子類中、子類的實例中都不能調用私有類型的屬性和方法
2.self 和 parent 的區別
a).在子類中常用到這兩個對像。他們的主要區別在於self可以調用父類中的公有或受保護的屬性,但parent不可以調用
b).self:: 它表示當前類的靜態成員(方法和屬性) 與 $this 不同,$this是指當前對像
附代碼:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
<?php
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
/**
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
* parent 只能調用父類中的公有或受保護的方法,不能調用父類中的屬性
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
* self 可以調用父類中除私有類型的方法和屬性外的所有數據
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
*/
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
class User{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
public $name;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
private $passwd;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
protected $email;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
public function __construct(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
//print __CLASS__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
$this->name= 'simple';
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
$this->passwd='123456';
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
$this->email = '
[email protected]';
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
public function show(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print "good ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
public function inUserClassPublic() {
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print __CLASS__.'::'.__FUNCTION__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
protected function inUserClassProtected(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print __CLASS__.'::'.__FUNCTION__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
private function inUserClassPrivate(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print __CLASS__.'::'.__FUNCTION__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
class simpleUser extends User {
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
public function __construct(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
//print __CLASS__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
parent::__construct();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
public function show(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print $this->name."//public ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print $this->passwd."//private ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print $this->email."//protected ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
public function inSimpleUserClassPublic() {
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print __CLASS__.'::'.__FUNCTION__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
protected function inSimpleUserClassProtected(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print __CLASS__.'::'.__FUNCTION__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
private function inSimpleUserClassPrivate() {
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print __CLASS__.'::'.__FUNCTION__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
class adminUser extends simpleUser {
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
protected $admin_user;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
public function __construct(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
//print __CLASS__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
parent::__construct();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
public function inAdminUserClassPublic(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print __CLASS__.'::'.__FUNCTION__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
protected function inAdminUserClassProtected(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print __CLASS__.'::'.__FUNCTION__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
private function inAdminUserClassPrivate(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print __CLASS__.'::'.__FUNCTION__." ";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
class administrator extends adminUser {
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
public function __construct(){
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
parent::__construct();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
/**
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
* 在類的實例中 只有公有屬性和方法才可以通過實例化來調用
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
*/
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
$s = new administrator();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
print '-------------------';
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
$s->show();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709231979.gif)
?>