码迷,mamicode.com
首页 > Web开发 > 详细

PHP 面向对象语法细节

时间:2017-03-29 11:25:31      阅读:236      评论:0      收藏:0      [点我收藏+]

标签: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 B
    17 {
    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 面向对象语法细节

标签:ack   general   ict   fine   blog   res   another   rom   long   

原文地址:http://www.cnblogs.com/jade640/p/6638554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!