From: halmai at nexum dot hu Operating system: Linux PHP version: 4.3.10 PHP Bug Type: Feature/Change Request Bug description: $this in static context
Description: ------------ I have two classes with one function respectively. In any function the variable $this should mean - the instance of the class if this function was called as a function of an instance of _that_ class, and - it should be undefined (null) if called as a static function. My problem is that if I call a static function of a class A from a non-static function of class B then in the member function of class A the variable $this denotes the instance of the class B instead of being null. The fact that in a function of class A the variable $this denotes an instance of class B is really confusing. If it was called statically then $this should be null. I know that this is described as normal behaviour but I think this is a useless and senseless feature. In case of accepting my suggestion it would be easy to decide whether a function was called in a static or non-static context. Reproduce code: --------------- class A { function what_am_i() { if ( $this === null ) { print "i am not an instance<br>"; } else { print "i am an instance from the class '".get_class( $this )."'.<br>"; } } } class B { function wrapper() { A::what_am_i(); } } A::what_am_i(); // i am not an instance (this is OK) $a = new A(); $a -> what_am_i(); // i am an instance from the class 'a'. (this is OK) $b = new B(); $b -> wrapper(); // i am an instance from the class 'b'. (this should output not an instance) Expected result: ---------------- i am not an instance i am an instance from the class 'a'. i am not an instance Actual result: -------------- i am not an instance i am an instance from the class 'a'. i am an instance from the class 'b'. -- Edit bug report at http://bugs.php.net/?id=33361&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=33361&r=trysnapshot4 Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=33361&r=trysnapshot50 Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=33361&r=trysnapshot51 Fixed in CVS: http://bugs.php.net/fix.php?id=33361&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=33361&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=33361&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=33361&r=needscript Try newer version: http://bugs.php.net/fix.php?id=33361&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=33361&r=support Expected behavior: http://bugs.php.net/fix.php?id=33361&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=33361&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=33361&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=33361&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=33361&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=33361&r=dst IIS Stability: http://bugs.php.net/fix.php?id=33361&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=33361&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=33361&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=33361&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=33361&r=mysqlcfg