首頁  >  文章  >  後端開發  >  public,private,protected的用法實例與區別分析

public,private,protected的用法實例與區別分析

伊谢尔伦
伊谢尔伦原創
2017-07-03 10:01:223080瀏覽

一,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    = &#39;public     <br>&#39;;
 $this->private   = &#39;private    <br>&#39;;
 $this->protected = &#39;protected  <br>&#39;;
 }
 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    = &#39;public     <br>&#39;;
 $this->private   = &#39;private    <br>&#39;;
 $this->protected = &#39;protected  <br>&#39;;
 }
 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 &#39;test1&#39;
 }
 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可以繼承。

以上是public,private,protected的用法實例與區別分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn