PHP 物件導向文法細節

來源:互聯網
上載者:User

標籤:ack   general   ict   fine   blog   res   another   rom   long   

  1. $this偽變數

    The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).
    As of PHP 7.0.0 calling a non-static method statically from an incompatible context results in $this being undefined inside the method. Calling a non-static method statically from an incompatible context has been deprecated as of PHP 5.6.0.
    As of PHP 7.0.0 calling a non-static method statically has been generally deprecated (even if called from a compatible context). Before PHP 5.6.0 such calls already triggered a strict notice.

    1. 偽變數$this在object上下文中方法被調用時有效,它是"the calling object"的引用,(通常,the calling object 是方法所屬的對象。但是,也可能是其他對象,如果方法是從次級對象(我的理解是子類對象)的上下文中靜態調用)。
    2. 如果靜態地調用一個非靜態方法,
    例子:
     1 <?php 2 class A 3 { 4     function foo() 5     { 6         if (isset($this)) { 7             echo ‘$this is defined (‘; 8             echo get_class($this); 9             echo ")\n";10         } else {11             echo "\$this is not defined.\n";12         }13     }14 }15 16 class B17 {18     function bar()19     {20         A::foo();21     }22 }23 24 error_reporting(E_ALL);25 26 $a = new A();27 $a->foo();28 29 A::foo();                //靜態調用非靜態方法30 $b = new B();31 $b->bar();                //從一個incompatibale context靜態調用非靜態方法32 33 B::bar();                 //靜態調用非靜態方法bar(),然後在bar()中再次靜態調用非靜態方法34 ?>
    php 5.5.38執行效果


    php 7.1.2執行效果

     

PHP 物件導向文法細節

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.