1. 程式人生 > >php class中public,private,protected的區別,以及實例

php class中public,private,protected的區別,以及實例

ons report acc per cnblogs 自己 context php undefined

一,public,private,protected的區別

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  
      
    ?>  

從上面的例子中,我們可以看出,
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();       //子類和父類有相同名字的屬性和方法,實例化子類時,子類的中的屬性和方法會蓋掉父類的。  
      
    ?>  

從上面的例子中,我們可以看出,
public:    test中的public可以被繼承。
private:   test中的private不可以被繼承。
protected:  test中的protected可以被繼承。
static:        test中的static可以被繼承。

唉,對於這些東西,老是不喜歡記著,用的時候,總感覺不對,又要去查,所以寫個例子,方便自己查看。

php class中public,private,protected的區別,以及實例