php的public、protected、private三種存取控制模式的區別 self,parent

來源:互聯網
上載者:User

標籤:io   使用   ar   資料   sp   cti   on   c   代碼   

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是指當前對像

附代碼:
<?php
/**
 * parent 只能調用父類中的公有或受保護的方法,不能調用父類中的屬性
 * self  可以調用父類中除私人類型的方法和屬性外的所有資料
 */
class User{
    public $name;
    private $passwd;
    protected $email;    
    public  function __construct(){
        //print __CLASS__." ";
        $this->name= ‘simple‘;
        $this->passwd=‘123456‘;
        $this->email = ‘[email protected]‘;
    }    
    public function show(){
        print "good ";
    }    
    public function inUserClassPublic() {
        print __CLASS__.‘::‘.__FUNCTION__." ";
    }    
    protected  function inUserClassProtected(){
        print __CLASS__.‘::‘.__FUNCTION__." ";
    }    
    private function inUserClassPrivate(){
        print __CLASS__.‘::‘.__FUNCTION__." ";
    }
}

class simpleUser extends User {    
    public function __construct(){        
        //print __CLASS__." ";
        parent::__construct();
    }
    
    public function show(){
        print $this->name."//public ";        
        print $this->passwd."//private ";
        print $this->email."//protected ";
    }
    
    public function inSimpleUserClassPublic() {
        print __CLASS__.‘::‘.__FUNCTION__." ";
    }
    
    protected function inSimpleUserClassProtected(){
        print __CLASS__.‘::‘.__FUNCTION__." ";
    }
    
    private function inSimpleUserClassPrivate() {
        print __CLASS__.‘::‘.__FUNCTION__." ";
    }
}

class adminUser extends simpleUser {
    protected $admin_user;
    public function __construct(){
        //print __CLASS__." ";
        parent::__construct();
    }
    
    public function inAdminUserClassPublic(){
        print __CLASS__.‘::‘.__FUNCTION__." ";
    }
    
    protected function inAdminUserClassProtected(){
        print __CLASS__.‘::‘.__FUNCTION__." ";
    }
    
    private function inAdminUserClassPrivate(){
        print __CLASS__.‘::‘.__FUNCTION__." ";
    }
}

class administrator extends adminUser {
    public function __construct(){        
        parent::__construct();
    }
}

/**
 * 在類的執行個體中 只有公有屬性和方法才可以通過執行個體化來調用
 */
$s = new administrator();
print ‘-------------------‘;
$s->show();
?>

php的public、protected、private三種存取控制模式的區別 self,parent

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.