|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-09-12 13:05 UTC] shamim at poetryunlimited dot com
Should I be able to do the following?
In the example below, class test1 is a base class for test2 which is a base class for test3.
In an object of type test1, if I call the class method test3::showMe, I would expect the class method test3::showMe would be called. This class method (not member function) would would have no class context ($this), since objects of type test1 do not have a member function test3::showMe.
This call should therefore fail at the debug call.
Instead, the call traverses up to test3, and the recursive parent::showMe calls reverse the traversal back to the class test1, even though class test1 has no knowledge of class test3 or class test2. Or so it should be. And for some reason the $this variable exists at every level. Even though objects of type test1 do not have any test3 or test2 object contexts.
I think the polymorphism is broken. Thanks.
Code follows.
===========================================================
<?php
Header("Content-Control: no-cache");
Header("Pragma: no-cache");
function debug(&$obj,$msg)
{
echo "$msg<br><pre>";var_dump($obj);echo "</pre><hr>\n";
}
class test1
{
var $a;
function test1() {$this->a=0;debug($this,"test1->new");}
function setA() {$this->a+=1;debug($this,"test1->setA");}
function display() {debug($this,"test1->display");echo $this->a."<br>\n";}
function callMe() {debug($this,"test1->callMe");test3::showMe();}
function showMe() {debug($this,"test1->showMe");$this->display();}
}
class test2 extends test1
{
function test2() {debug($this,"test2->new");$p=get_parent_class($this);parent::$p();}
function display() {debug($this,"test2->display");parent::display();}
function showMe() {debug($this,"test2->showMe");parent::showMe();}
}
class test3 extends test2
{
function test3() {debug($this,"test3->new");$p=get_parent_class($this);parent::$p();}
function display() {debug($this,"test3->display");parent::display();}
function showMe() {debug($this,"test3->showMe");parent::showMe();}
}
$b=new test2();
$b->setA();
$c=new test1();
$c->setA();
$a=new test1();
$a->showMe();
$a->callMe();
$a->setA();
$a->showMe();
$a->callMe();
$a->setA();
$a->showMe();
$a->callMe();
$a=new test2;
$a->showMe();
$a->callMe();
$a->setA();
$a->showMe();
$a->callMe();
$a->setA();
$a->showMe();
$a->callMe();
/*
Results:
test2->new
object(test2)(1) {
["a"]=>
NULL
}
--------------------------------------------------------------------------------
test1->new
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test1->setA
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test1->new
object(test1)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test1->setA
object(test1)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test1->new
object(test1)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test1->showMe
object(test1)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test1->display
object(test1)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
0
test1->callMe
object(test1)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test3->showMe
object(test1)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test2->showMe
object(test1)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test1->showMe
object(test1)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test1->display
object(test1)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
0
test1->setA
object(test1)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test1->showMe
object(test1)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test1->display
object(test1)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
1
test1->callMe
object(test1)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test3->showMe
object(test1)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test2->showMe
object(test1)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test1->showMe
object(test1)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test1->display
object(test1)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
1
test1->setA
object(test1)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test1->showMe
object(test1)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test1->display
object(test1)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
2
test1->callMe
object(test1)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test3->showMe
object(test1)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test2->showMe
object(test1)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test1->showMe
object(test1)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test1->display
object(test1)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
2
test2->new
object(test2)(1) {
["a"]=>
NULL
}
--------------------------------------------------------------------------------
test1->new
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test2->showMe
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test1->showMe
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test2->display
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test1->display
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
0
test1->callMe
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test3->showMe
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test2->showMe
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test1->showMe
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test2->display
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
test1->display
object(test2)(1) {
["a"]=>
int(0)
}
--------------------------------------------------------------------------------
0
test1->setA
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test2->showMe
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test1->showMe
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test2->display
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test1->display
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
1
test1->callMe
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test3->showMe
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test2->showMe
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test1->showMe
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test2->display
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
test1->display
object(test2)(1) {
["a"]=>
int(1)
}
--------------------------------------------------------------------------------
1
test1->setA
object(test2)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test2->showMe
object(test2)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test1->showMe
object(test2)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test2->display
object(test2)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test1->display
object(test2)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
2
test1->callMe
object(test2)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test3->showMe
object(test2)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test2->showMe
object(test2)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test1->showMe
object(test2)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test2->display
object(test2)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
test1->display
object(test2)(1) {
["a"]=>
int(2)
}
--------------------------------------------------------------------------------
*/
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 19:00:01 2025 UTC |
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(); ?>