Home » Php » oop – scope of $this is funked in PHP is it a bug or a feature?

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&Answers:

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.