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
Why don’t you just remove the
static
offprint
? 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.– utluiz