class A {
public $a = "BooMBa";
public function fun1() {
echo $this->a;
// echo (new self)->a;
}
}
class B extends A {
public static function fun2() {
return (new self)->fun1();
// return static::fun1();
}
}
B::fun2(); // returns BooMBa
If I change fun2() to return static::fun1()
then it gives
'Fatal Error: Using $this when not in object context'But if I change fun1 to
echo (new self)->a;
then works fine.Can someone explain why
static::
hate $this
but not (new self)
.PS: Please consider that I went through these below given Q&A sections and some more articles yet couldn't get an exact hold of this particular scenario that what's going on.
What does new self(); mean in PHP?
PHP : Difference b/w new self and new object()
PHP: The Basics Manual
in PHP, what is the difference between self and $this?
PPS: This Q&A PHP Fatal error: Using $this when not in object context has nothing to do with mine other than the same error message. It's totally different context. In that question he simply calls a non-static method statically. Meanwhile my question is more complex with late static bindings and keywords (new self). It's not possible at all to understand my problem with the answer in the above mentioned Q&A so please go through both Q&A before marking it duplicate. Thank you.