Preventing static method calls in PHP4
| From: | Marcus Bointon | Date: | Thu, 22 Jul 2004 08:00:39 +0000 |
| Subject: | Preventing static method calls in PHP4 | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-191484@lists.php.net to get a copy of this message | ||
[repost, not sure if it worked last time]
If I want to prevent a method from being called statically in PHP4, how can
I do it? So far I've tried these techniques:
<?php
class A {
var $Anotstatic;
function A() {
$this->Anotstatic = true;
}
function dynamic() {
if (!isset($this) or !is_a($this, 'A') or !(isset($this->Anotstatic) and
$this->Anotstatic))
echo "Method called statically\n";
else
echo "dynamic-only function\n";
}
function test ()
{
A::dynamic();
}
}
class B {
function test() {
A::dynamic();
}
}
$a = new A();
$a->dynamic();
A::dynamic();
$b = new B;
$b->test();
$a->test();
?>
This works for the first 3 tests (so I'm getting there), but not the fourth,
that is a dynamic method being called statically from an instance of the
same class. Is there something I've missed that will allow me to intercept
this style of call?
I know that this problem goes away in PHP5, and that the setting of $this in
static calls from other instances is not a bug (though it's the root of this
problem)!
Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
marcus@synchromedia.co.uk | http://www.synchromedia.co.uk