|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2005-06-16 13:28 UTC] johannes@php.net
[2005-06-16 13:39 UTC] halmai at nexum dot hu
[2005-06-16 13:52 UTC] halmai at nexum dot hu
[2005-06-16 13:57 UTC] tony2001@php.net
[2005-06-16 14:02 UTC] halmai at nexum dot hu
[2005-06-16 14:08 UTC] tony2001@php.net
[2005-06-16 14:33 UTC] halmai at nexum dot hu
[2005-06-16 14:39 UTC] tony2001@php.net
[2005-06-16 14:58 UTC] halmai at nexum dot hu
[2005-06-16 15:14 UTC] tony2001@php.net
[2005-06-16 15:21 UTC] halmai at nexum dot hu
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 18:00:01 2025 UTC |
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'.