#20089 [Bgs]: Calling static method from instance method results in invalid $this
| From: | helly@php.net | Date: | Sun, 25 Apr 2004 16:12:26 +0000 |
| Subject: | #20089 [Bgs]: Calling static method from instance method results in invalid $this | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-58730@lists.php.net to get a copy of this message | ||
ID: 20089
Updated by: helly@php.net
Reported By: dougqh at incogen dot com
Status: Bogus
Bug Type: Scripting Engine problem
-Operating System: Solaris
+Operating System: *
PHP Version: 4.2.3
New Comment:
Not on PHP5: In a stattically called method there cannot be a $this
because it is a local variable generated by the engine. The behavior of
PHP 4 is wrong and is there only because there simply was no way to
implement it correct. One of the reasons it could not be done correct
is the lack of real static methods.
Previous Comments:
------------------------------------------------------------------------
[2002-10-25 12:53:42] dougqh at incogen dot com
Maintaining $this within a class even when a static method is called is
reasonable, but allowing a $this to exist that is not from the proper
class is awful. At the very least, I think this problem should be
better documented. A warning in the manaul entry for ::
(http://www.php.net/manual/en/keyword.paamayim-nekudotayim.php) would
be good, since that page currently incorrectly indicates that $this
will not be available when a static method call is made. I see a user
note has been added, but something more visible would be preferable.
------------------------------------------------------------------------
[2002-10-25 12:03:35] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Also, this was discussed before (in both the bugdatabase and on the
developer mailinglist) and this behavior will not be changed in the
near future.
------------------------------------------------------------------------
[2002-10-25 11:08:54] dougqh at incogen dot com
Here is an example ...
<?php
class Foo
{
function Foo()
{
$this->class = 'foo';
}
function doCall()
{
return Bar::staticFunction();
}
};
class Bar
{
function staticFunction()
{
if ( isset( $this ) )
{
print 'This isset <br />';
print_r( $this );
}
else
{
print 'Call as a static <br />';
}
}
};
$foo =& new Foo();
$foo->doCall();
?>
Note an instance of Foo is calling a static method of Bar. Bar does a
test to see if $this is set. $this should not be set, since the method
was statically invoked. Unfortunately, $this is set and its the
instance of Foo that made the call to the static method of Bar.
This bug makes writing methods that work both as static methods and
instance methods virtually impossible. It is also extremely unsafe to
allow Bar to access the member variables of Foo.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=20089&edit=1