开发者

scope of $this is funked in PHP is it a bug or a feature?

开发者 https://www.devze.com 2023-04-06 23:46 出处:网络
I have this code: class a(){ function b(){ if(isset($this){ echo \'instance! \'; echo get_class($this); }else{

I have this code:

    class a(){
      function b(){
         if(isset($this){
            echo 'instance! ';
            echo get_class($this);
         }else{
            echo 'static';
         }
      }
    }


class C{
  public function test(){
      a::b();
  }
}

$CC=new C;
$CC->test();

This will echo

开发者_开发技巧instance C


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).

source

So definitely, it's a feature, it's by design, and it's not a bug.

0

精彩评论

暂无评论...
验证码 换一张
取 消