#19376 [Opn->]: Class inheritance problem

From: Date: Sat, 14 Sep 2002 09:17:43 +0000
Subject: #19376 [Opn->]: Class inheritance problem
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-19336@lists.php.net to get a copy of this message
ID: 19376 Updated by: derick@php.net Reported By: shamim@poetryunlimited.com -Status: Open +Status: Won't fix Bug Type: Scripting Engine problem Operating System: Linux/Windows NT 4 PHP Version: 4.2.2 New Comment: This is just how it is implemented, and it can now not be changed because of backward compatibility -> won't fix. Derick Previous Comments: ------------------------------------------------------------------------ [2002-09-13 00:04:20] shamim@poetryunlimited.com This is not the same as the one in the documentation that you sent me. In the documentation, you are calling A::example directly, or calling B::example which calls up the chain to A. In that context, it's fine. Objects of type B contain an A context. It's when you call B::example from within A::example that all hell breaks loose. Objects of type A have no B context. Example. <?php error_reporting(E_ALL); class A { var $a; // This should be a static call - the only way it makes sense. function example(){echo $this->a;B::Example();} } class B extends A { var $b; // This should fail when called from objects of type A. // And it does, but only because b is undefined, not // because $this is undefined. But since an object of // type A called this static method, $this should not // exist. The error should be undefined variable $this // not undefined property b. function example(){echo $this->b;} } $a=new A; $a->example(); ?> --------------- My 3 class script below also illustrates that not only is the class context passed out of the current object to a static method, when the function call chain returns to the original class, which should have no context, the original context is still carried. I really don't see how this is documented. Is there any further documentation? You're suggesting I subscribe to php-dev?? Duplicated below w/o the results. --------------- <?php // Example of bug in PHP class method passing $this incorrectly class test1 { var $a; function test1() function showMe() { // Since test3::showMe was called as a static method // This too should be a static method call with no $this echo 'In test1::showMe<hr>'; echo 'Next 2 lines should fail since this method was not called from within this object<br>'; echo '$this is of type '. get_class($this)." in test3::showMe<br>\n"; echo "test1::showMe:a=".$this->a."<br>\n"; } // Class member to test static method call function callMe() { echo 'In test1::callMe<hr>'; echo '$this is of type '.get_class($this)."in test1::callMe<hr>\n"; echo 'Calling class method test3::showMe from an object of type test1<br>'; echo '$this should not be passed since test3::showMe does not exist in objects of type test1<hr>'; // This is a static method call, since test1 objects // do not have a test3::showMe. test3::showMe(); } } class test2 extends test1 { function showMe() { echo 'In test2::showMe<br>'; echo "Next 2 lines should fail since objects of type test1 cannot pass \$this<hr>\n"; echo '$this is of type '. get_class($this)." in test2::showMe<br>\n"; echo "test2::showMe:a=".$this->a."<hr>\n"; echo 'Calling parent::showMe<hr>'; // Since test3::showMe was called as a static method // This too should be a static method call with no $this parent::showMe(); } } class test3 extends test2 { function showMe() { echo 'In test3::showMe<br>'; echo "Next 2 lines should fail since objects of type test1 cannot pass \$this<hr>\n"; echo '$this is of type '. get_class($this)." in test3::showMe<br>\n"; echo "test3::showMe:a=".$this->a."<hr>\n"; echo 'Calling parent::showMe<hr>'; // Since test3::showMe was called as a static method // This too should be a static method call with no $this parent::showMe(); } } // object $a is of type test1 $a=new test1; $a->callMe(); ?> ------------------------------------------------------------------------ [2002-09-12 23:50:43] alan@akbkhome.com oops forgot to mention - its documented http://www.php.net/manual/en/keyword.paamayim-nekudotayim.php "In this context, there is a current object and it may have object variables. Thus, when used from WITHIN an object function, you may use $this and object variables." ------------------------------------------------------------------------ [2002-09-12 23:45:09] alan_k@php.net dupe of http://bugs.php.net/bug.php?id=12622 I'm not sure if it will be fixed.. nobody has commented on 12622 It may be worth posting a question on php-dev, just to get a more definitive answer... ------------------------------------------------------------------------ [2002-09-12 23:33:07] shamim@poetryunlimited.com Please see bug report http://bugs.php.net/bug.php?id=19384 for further details. You say that static method calls take $this with them? But how can they take them OUT OF the current class? Which $this is in effect in the class of the static method?? What happens if the static method call has it's own variable of the same name or function? I did not see anywhere in the documentation that states that static method calls take $this with them, even out of the current class. I'm confused. How can an object of type A call a static method of class B and have $this taken with it??? This violates class scoping as far as I understand it. I don't see how this would enable multiple inheritance, especially since PHP is supposed to be single inheritance anyway. You're saying this isn't a bug? Isn't that like someone telling me a year ago that &new was necessary and it wouldn't be fixed because it was a feature, and now Zend 2.0 will do it by default??? Suggestions? Where should I take this issue? And where do I find the documentation that clearly describes the expected behavior. Thanks. ------------------------------------------------------------------------ [2002-09-12 23:25:15] alan_k@php.net apologies for the brush of effect. if you could mark the first instance where you think the behaviour is unexpected it would help. I assume you realize that all static method calls take with them the '$this' value of the calling objects (always). It's a feature, and It has a few usefull side effects, like making a sudo multiple inheritance possible... ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/19376 -- Edit this bug report at http://bugs.php.net/?id=19376&edit=1

« previous php.bugs (#19336) next »