美文网首页PHP Chaos
PHP 5 与 PHP 7 中 $this 差异

PHP 5 与 PHP 7 中 $this 差异

作者: xiaojianxu | 来源:发表于2017-07-28 15:11 被阅读8次
<?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.

相关文章

网友评论

    本文标题:PHP 5 与 PHP 7 中 $this 差异

    本文链接:https://www.haomeiwen.com/subject/vptflxtx.html