Why can’t I use $this within a Static class?

Asked

Viewed 322 times

6

As an example below, I was wondering why I can’t use $this within a Static class?

<?php

class A{
     public static function hello(){
        echo 'hello';
    }
}

class B extends A{  
    public function ok(){
        echo 'ok';
    }

    public static function fprint(){
        A::hello();
        $this->ok();
    }   
}

$obj = new B;
$obj->fprint();
?>

The problem is in the method fprint. I understand that a Static method can be used without the need for an object, but if I call an object, as I did, the method fprint do not need to use it to call the method hello, because I use Class A for that, and $this will serve to call the method ok with the object of the instance I created. I don’t understand why this gives error.

The error returned:

Fatal error: Using $this when not in Object context

  • 1

    Why don’t you just remove the static of fprint? The idea of a static method is just not to depend on the object. Once you want to use the object, use a normal instance method.

2 answers

8


The $this represents the instance of a class. You should not use it for static methods, simply because there is no concept of instance in them. In fact, the reason to use a static method is the independence of an instantiated object.
(also mentioned by @utluiz almost the same instant I posted.)

The question is another: if you need the instance, why declare the method with Static?

Note that this works normally. I even passed the Static for the top method for you to see the scope resolution syntax :: that you used in B::ok() in action (see note at the end):

<?php

class A{
     public static function hello(){
        echo 'hello';
    }
}

class B extends A{  
    public static function ok(){
        echo 'ok';
    }

    public function fprint(){
        A::hello();
        $this->ok();
    }   
}

$obj = new B;
$obj->fprint();
?>

See working on IDEONE.

Another example:

<?php

class A{
     public static function hello(){
        echo 'hello';
    }
}

class B extends A{  
    public function ok(){
        echo 'ok';
    }

    public static function fprint(){
        A::hello();
        B::ok();
    }   
}

$obj = new B;
$obj->fprint();
?>

See this version on IDEONE.

This way is also working in IDEONE, because we are no longer using the instance ($this), but making a new call, this time static, to the method ok().

Important note: in the second example the so-called static will only work without the method being static if the mode E_STRICT is not active. This is an operation that can be allowed for compatibility with older versions of PHP, the reverse is not. In a normal situation, you would have both static methods.

Some references in the manual:
Paamayim Nekudotayim
Static
Classes

2

Basically you can’t use $this directly in static methods because they are not directly connected to objects or instances of the class, while non-static methods always refer to objects or instances of that class. In response to this, you can define your method/skill as static so you can use it in static methods.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.