$this
php.net
1 <?php 2 3 4 class A 5 { 6 function foo() 7 { 8 if (isset($this)) { 9 echo '$this is defined (',get_class($this),')'."\n"; 10 } else { 11 echo '$this is not defined.'."\n"; 12 } 13 } 14 } 15 16 class B 17 { 18 function bar() 19 { 20 A::foo(); 21 } 22 } 23 24 $a = new A(); 25 $a->foo(); 26 27 A::foo(); 28 $b = new B(); 29 $b->bar(); 30 31 B::bar();
$this is defined (A)
( ! ) Strict standards: Non-static method A::foo() should not be called statically in D:\wamp64\www\w0827pm\w.php on line 27 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0020 | 241448 | {main}( ) | ...\w.php:0 |
$this is not defined.
( ! ) Deprecated: Non-static method A::foo() should not be called statically, assuming $this from incompatible context in D:\wamp64\www\w0827pm\w.php on line 20 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0020 | 241448 | {main}( ) | ...\w.php:0 |
2 | 0.0030 | 242352 | B->bar( ) | ...\w.php:29 |
$this is defined (B)
( ! ) Strict standards: Non-static method B::bar() should not be called statically in D:\wamp64\www\w0827pm\w.php on line 31 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0020 | 241448 | {main}( ) | ...\w.php:0 |
( ! ) Strict standards: Non-static method A::foo() should not be called statically in D:\wamp64\www\w0827pm\w.php on line 20 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0020 | 241448 | {main}( ) | ...\w.php:0 |
2 | 0.0030 | 242352 | B::bar( ) | ...\w.php:31 |
$this is not defined.
/*
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).
As of PHP 7.0.0 calling a non-static method statically from an incompatible context results in $this being undefined inside the method. Calling a non-static method statically from an incompatible context has been deprecated as of PHP 5.6.0. As of PHP 7.0.0 calling a non-static method statically has been generally deprecated (even if called from a compatible context). Before PHP 5.6.0 such calls already triggered a strict notice.
*/