Bug 12622 Addressed in PHP 4.2.4?? Addressed in Zend 2.0??
| From: | Shamim Islam | Date: | Fri, 13 Sep 2002 18:25:22 +0000 |
| Subject: | Bug 12622 Addressed in PHP 4.2.4?? Addressed in Zend 2.0?? | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-88310@lists.php.net to get a copy of this message | ||
I have discovered this problem recently with my own work with classes and objects
in PHP. It appears that something that is incorrect does not fail when it should.
The original bug is listed at http://bugs.php.net/bug.php?id=12622
I discovered this in the process of posting my own bug report on this problem.
PHP should catch these errors, IMHO as opposed to us spending time finding them.
Also, is there anywhere in the documentation that correctly explains the semantics
of when the $this context is passed to static method calls?
I feel that to be in line with OO doctrines, any object should be able to call any
method in its class hierarchy as if it were a class method call (including parent)
(pass the $this) and any method not in its class hierarchy as a static method call
(do not pass $this).
This is illustrated below.
Comments???
==========================================================
The following fails, but for the wrong reason.
==========================================================
<?php
error_reporting(E_ALL);
class A
{
var $a;
// Making a static call to class B - 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.
// but not because b as a property is missing but because
// $this is undefined in this context.
function example(){echo $this->b;}
}
$a=new A;
$a->example();
?>
==================================================================
The following DOES NOT fail, but it should. Causes a silent error
==================================================================
<?php
error_reporting(E_ALL);
// 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();
?>