8

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

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
  • 1
    http://stackoverflow.com/questions/151969/php-self-vs-this – dm03514 Sep 24 '11 at 14:04
  • I think it's a hybrid between a bug and a feature... There's a dupe about this, maybe I can find it – Pekka Sep 24 '11 at 14:04
  • 1
    @md03514 your point being? Itay understands the difference between the two. Look at the code (edit: Ah, there are some in-depth explanations there that may explain things.) – Pekka Sep 24 '11 at 14:05
  • I had a similar question http://stackoverflow.com/questions/516355/calling-static-method-from-class-bwhich-extends-class-a-of-class-a. From Ulf: "If you call the static method bound to the other object, the method is executed in the context of the current object. Which allows access to the $this-object." – Mike B Sep 24 '11 at 14:05

1 Answers1

5

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.

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89