PHP 5 与 PHP 7 中 $this 差异

<?php

  class A
  {
      function foo()
      {
          if (isset($this)) {
              echo '$this is defined (';
              echo get_class($this);
              echo ")\n";
          } else {
              echo "\$this is not defined.\n";
          }
    }
}

 class B
 {
      function bar()
      {
          A::foo();
      }
  }

  $a = new A();
  $a->foo();

  A::foo();
  $b = new B();
  $b->bar();

  B::bar();
?>

Output of the above example in PHP 5:
$this is defined (A)
$this is not defined.
$this is defined (B)
$this is not defined.

Output of the above example in PHP 7:
$this is defined (A)
$this is not defined.
$this is not defined.
$this is not defined.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 11,413评论 0 23
  • 对于一个正在学翻译专业的大一新生,我真的不清楚当初干嘛要报翻译这个专业。英语那么烂的我,不远千里来到这里,是为了听...
    李子呐阅读 490评论 8 0
  • 但是我今天收到了这个,特别特别开心。 在这个世界上,你一定要每时每刻都带着感恩的心哦。 晚安,坚持哦。
    有点困的滚滚阅读 270评论 0 2

友情链接更多精彩内容