#20089 [Com]: Calling static method from instance method results in invalid $this
| From: | dougqh at incogen dot com | Date: | Fri, 25 Oct 2002 17:53:42 +0000 |
| Subject: | #20089 [Com]: Calling static method from instance method results in invalid $this | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-23334@lists.php.net to get a copy of this message | ||
ID: 20089
Comment by: dougqh@incogen.com
Reported By: dougqh@incogen.com
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Solaris
PHP Version: 4.2.3
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[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@incogen.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