ManiacDan Posted October 9, 2012 Share Posted October 9, 2012 Consider the following code: <?php error_reporting( E_ALL | E_STRICT ); class staticClass { function staticMethod() { //when called statically from the global scope, this throws a fatal error due to $this echo $this->echoMe . "\n"; } } class dynamicClass { public $echoMe; public function __construct( $echoMe ) { $this->echoMe = $echoMe; } public function thisShouldNotWork() { //calling the staticClass method statically does NOT throw a fatal error as expected, the $this reference inside //staticClass is assumed to be a reference to the current instance of dynamicClass instead staticClass::staticMethod(); } } $a = new dynamicClass( "Hello, World!" ); $a->thisShouldNotWork(); /* The above throws a strict warning BUT STILL PRINTS "Hello, World!" Strict Standards: Non-static method staticClass::staticMethod() should not be called statically, assuming $this from incompatible context in test.php on line 22 Hello, World! */ The aptly named function thisShouldNotWork calls a method statically when that method was not defined as static. Even though the function is called statically, $this exists inside that function and can be manipulated and accessed. However, $this inside of staticClass points to a different class. No inheritance is given by the code, but one class is able to access another's variables using $this. PHP "falls back" to the last valid instance of $this since it's a super-global. Only a strict warning level will tell you that something wacky is going on. Quote Link to comment https://forums.phpfreaks.com/topic/269261-interesting-behavior-in-phps-handling-of-this/ Share on other sites More sharing options...
xyph Posted October 9, 2012 Share Posted October 9, 2012 Well, wacky code will produce wacky results. PHP being so forgiving, it doesn't just explode - There are definitely positives and negatives of that overall behaviour. Quote Link to comment https://forums.phpfreaks.com/topic/269261-interesting-behavior-in-phps-handling-of-this/#findComment-1384079 Share on other sites More sharing options...
kicken Posted October 9, 2012 Share Posted October 9, 2012 (edited) Calling a not-declared-static method statically not causing an error is a backward compatibility thing I know (for older php4 code). Why they chose to import $this from a different context thought I don't know. Maybe php4 behaved like that, I've never tried it. Someone certainly might be confused by the behavior if they are not running with full error reporting. At least it handles the context right w/ respect to private/protected members and disallows access. Edited October 9, 2012 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/269261-interesting-behavior-in-phps-handling-of-this/#findComment-1384087 Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Author Share Posted October 10, 2012 I didn't really want to say this because I don't like giving people bad ideas, but this is a way to fake multiple inheritance or a sort of fake-y interface that doesn't require additional coding on the interfaced objects. It's still definitely weird and you shouldn't ever do it, but it's interesting, especially since it respects the invalid context and doesn't allow private/protected access. It knows that $this is another object, but uses it anyway. Such an odd choice. Quote Link to comment https://forums.phpfreaks.com/topic/269261-interesting-behavior-in-phps-handling-of-this/#findComment-1384110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.