<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                https://blog.csdn.net/tham_/article/details/42041929 php中this,self,parent三個關鍵字的作用 this,self,parent三個關鍵字之間的區別,從字面上比較好理解,分別是指這、自己、父親。我們先建立幾個概念,這三個關鍵字分別是用在什么 地方呢?我們初步解釋一下,this是指向當前對象的指針(姑且用C里面的指針來看吧),self是指向當前類的指針,parent是指向父類的指針。我 們這里頻繁使用指針來描述,可能是因為沒有更好的語言來表達。 <?php <span style="color:#ff0000;">// this是指向當前對象的指針</span> class test_this{ private $content; //定義變量 function __construct($content){ //定義構造函數 $this->content= $content; } function __destruct(){}//定義析構函數 function printContent(){//定義打印函數 echo $this->content.'<br />'; } } $test=new test_this('北京歡迎你!'); //實例化對象 $test->printContent();//北京歡迎你! $test=new test_this('新年新氣象!');//再一次實例化對象 $test->printContent();//新年新氣象! echo '<br />'; <span style="color:#ff0000;">//self是指向類的本身,只跟類有關,跟任何對象實例無關</span> class test_self{ private static $first_count; //定義靜態變量 private $last_count; function __construct(){ $this->last_count=++self::$first_count;//直接用self調用變量的值賦值給另一個變量 } function __destruct(){} function print_self(){ print($this->last_count); } } $abc=new test_self();//實例化對象 $abc->print_self();//1 echo '<br />'; <span style="color:#ff0000;">//parent是指向父類的指針</span> class test_parent{ //基類 public $name; //定義姓名 父類成員需要定義為public,才能夠在繼承類中直接使用 this來調用。 function __construct($name){ $this->name=$name; } } class test_son extends test_parent{ //派生類 繼承test_parent public $gender;//定義性別 public $age; //定義年齡 function __construct($gender,$age){ //繼承類的構造函數 parent::__construct('nostop');//使用parent調用父類的構造函數,來進行對父類的實例化 $this->gender=$gender; $this->age=$age; } function __destruct(){} function print_info(){ echo $this->name.'是個'.$this->gender.',今年'.$this->age.'歲'.'<br />'; } } $nostop=new test_son('女性','22');//實例化test_son對象 $nostop->print_info();//執行輸出函數 nostop是個女性,今年23歲 ?> $this $this表示當前實例,在類的內部方法訪問未聲明為const及static的屬性時,使用$this->value='phpernote';的形式。常見用法如: $this->屬性 $this->方法 舉例如下: 查看代碼打印 <span style="font-size:14px;"><?php class MyClass{ private $name; public function __construct($name){ $this->name=$name; } public function getname(){ return $this->name; } public function printName(){ echo $this->getname(); } } $myclass= new MyClass("I Like PHP"); $myclass->printName();//輸出:I Like PHP ?></span> 在類里面調用當前類的屬性和方法有三種方法,分別是self、parent、$this,這三個關鍵字的區別是:self用來指向當前的類;parent用于指向當前類的父類,可以使用該關鍵字調用父類的屬性和方法;$this用來在類體內調用自身的屬性和方法。 static 關鍵字可以是self(在類內部調用靜態成員時所使用)靜態成員所在的類名(在類外調用類內部的靜態成員時所使用) 聲明一個靜態變量如下: static $val=''; 只存在于函數作用域的變量,函數執行之后變量的值不會丟失,只會初始化一次,初始化靜態變量不能使用表達式,不用全局變量代替是因為全局變量會被所有函數訪問容易造成維護不宜 。 在 類中使用static有兩種主要用途、定義靜態成員和定義靜態方法。靜態成員只保留一個變量的值,這個值對所有實例都是有效的 ,如下: <?php class MyObject{ public static $myStaticVar=0; function myMethod(){ self::$myStaticVar+=2; echo self::$myStaticVar; } } $instance1=new MyObject(); $instance1->myMethod(); $instance2=new MyObject(); $instance2->myMethod(); //結果將分別打印2、4 <?php class Book{ static $num=0; public function showMe(){ echo"您是滴".self::$num."位訪客"; self::$num++; } } $book1=new Book(); $book1->showMe(); echo"<br>"; $book2=new Book(); $book2->showMe(); echo"<br>"; echo"您是滴".Book::$num."位訪客"; ?> 結果將是: 您是滴0位訪客 您是滴1位訪客 您是滴2位訪客 另外需要注意的是如果類的方法是static的,他所訪問的屬性也必須是static的。 final 最終的類和方法,不能繼承,該關鍵字修飾的方法不能被重寫。一般用法如下: <?php final class MyClass{//此類將不允許被繼承 final function fun1(){......}//此方法將不允許被重寫 } const 在類的 內部方法訪問已經聲明為const及static的屬性時,需要使用self::$name的形式調用 。舉例如下: <?php class clss_a{ private static $name="static class_a"; const PI=3.14; public $value; public static function getName(){ return self::$name; } //這種寫法有誤,靜態方法不能訪問非靜態屬性 public static function getName2(){ return self::$value; } public function getPI(){ return self::PI; } } 注意const屬性的申明格式是const PI=3.14,而不是const $PI=3.14。 self self表示類本身,指向當前的類。通常用來訪問類的靜態成員、方法和常量。 PHP中 :: 、-> 、self 、$this操作符的區別 在訪問PHP類中的成員變量或方法時,如果被引用的變量或者方法被聲明成const(定義常量)或者static(聲明靜態),那么就必須使用操作符::,反之如果被引用的變量或者方法沒有被聲明成const或者static,那么就必須使用操作符->。 另外,如果從類的內部訪問const或者static變量或者方法,那么就必須使用自引用的self,反之如果從類的內部訪問不為const或者static變量或者方法,那么就必須使用自引用的$this。 PHP雙冒號::的用法 雙冒號操作符即作用域限定操作符Scope Resolution Operator可以訪問靜態、const和類中重寫的屬性與方法。 在類定義外使用的話,使用類名調用。在PHP 5.3.0,可以使用變量代替類名。Program List:用變量在類定義外部訪問。 <?php class Fruit { const CONST_VALUE = 'Fruit Color'; } $classname = 'Fruit'; echo $classname::CONST_VALUE; // As of PHP 5.3.0 echo Fruit::CONST_VALUE; ?> Program List:在類定義外部使用:: <?php class Fruit { const CONST_VALUE = 'Fruit Color'; } class Apple extends Fruit { public static $color = 'Red'; public static function doubleColon() { echo parent::CONST_VALUE . "\n"; echo self::$color . "\n"; } } 程序運行結果:Fruit Color Red Program List:調用parent方法 <?php class Fruit { protected function showColor() { echo "Fruit::showColor()\n"; } } class Apple extends Fruit { // Override parent's definition public function showColor() { // But still call the parent function parent::showColor(); echo "Apple::showColor()\n"; } } $apple = new Apple(); $apple->showColor(); ?> 程序運行結果: Fruit::showColor() Apple::showColor() Program List:使用作用域限定符 <?php class Apple { public function showColor() { return $this->color; } } class Banana { public $color; public function __construct() { $this->color = "Banana is yellow"; } public function GetColor() { return Apple::showColor(); } } $banana = new Banana; echo $banana->GetColor(); ?> 程序運行結果:Banana is yellow Program List:調用基類的方法 <?php class Fruit { static function color() { return "color"; } static function showColor() { echo "show " . self::color(); } } class Apple extends Fruit { static function color() { return "red"; } } Apple::showColor(); // output is "show color"! ?> 程序運行結果:show color ———————————————— 版權聲明:本文為CSDN博主「tham_」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/tham_/article/details/42041929
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看