Re: Static weirdness..
| From: | Greg Beaver | Date: | Thu, 19 Feb 2004 00:38:17 +0000 |
| Subject: | Re: Static weirdness.. | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-7980@lists.php.net to get a copy of this message | ||
Hi,
I agree that allowing $a->bar() with a static method is too confusing,
and should not be allowed. However, the ability to call a static method
of an object (variable class name, in other words), is invaluable. What
if PHP simply allowed $object::staticMethod() syntax?
I did some playing around, and although I wouldn't know how to disable
$a->staticMethod(), this patch enables $a::staticMethod(). It also has
another side effect
<?php
class foo {
static function bar()
{
echo 'hello';
}
}
$bar = 'hello';
$a = new foo;
$a->bar(); // this could be disabled to error out easily
$a::bar(); // my patch now allows this to print "hello"
$a::$bar(); // this also works
$a::{'bar'}(); // even this works
?>
If someone could get the patch to work with a T_STRING instead of an
object_property, then it would disable the $a::$bar() and
$a::{expression}() syntax.
This is my first attempt at a patch of the core, please be gentle if it
sucks. :)
Thanks,
Greg
John Coggeshall wrote:
> http://bugs.php.net/bug.php?id=27304
>
> Marcus says he's brought this up before, and i think it really needs to
> be addressed before PHP 5 so I'm bringing it up again. I am told that
> currently we are allowing static methods to be called from an object
> context because of a performance hit if we check every call, but
> currently because $this is undefined regardless of context there is no
> way even for the developer to check if the method was called properly.
>
> John
>