This is a part of a Zend Newsletter Issue # 218:
------------------------------------------------------------------
Gareth Ardron kicked off the week with a request for a sanity check over PHP 5.0.3 behaviour:
step one:
- Code: Select all
$input = "foo";
$scanning_class = "clamav";
$result = $scanning_class::scanBuffer($input);
- fails with Parse error: parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM
So ok, you can't put variables at the front on a class call like that.
Minor bug, I'm thinking at this point, but I wonder if I can work round it. So, onto step two:
- Code: Select all
$input = "foo";
$scanning_class = "clamav";
$func = $scanning_class."::scanBuffer";
$result = $func($input);
- fails with Fatal error: Call to undefined function clamav::scanBuffer()
Just doing:
- Code: Select all
$result = clamav::scanBuffer($input);
of course works absolutely fine.
Somebody just tell me that this isn't exactly expected behaviour and it's a minor bug?
Nicolas Bérard Nault replied, saying that this appeared to be a Zend engine limitation to him. In 'step two', Gareth was literally calling a function named clamav::scanBuffer() rather than a member function of the class clamav.
Adam Maccabee Trachtenberg referred to it as 'a known limitation' and suggested using the Reflection classes to work around it:
- Code: Select all
class foo {
static function bar() {
print "static method!\n";
}
}
$class = 'foo';
$method = 'bar';
$x = new ReflectionMethod($class, $method);
$x->invoke(NULL);
Someone named Chris agreed that it was expected behaviour, and suggested using:
- Code: Select all
call_user_func(array($scanning_class,'scanBuffer'));
Jason Sweat offered another option:
- Code: Select all
eval("\$result = $scanning_class::scanBuffer(\$input);");
Gareth thanked everyone for their replies and threatened to have 'a bit of a prod at the internals', explaining that he couldn't use sophisticated options such as the Reflection API because his code would need to be maintained by people who hadn't yet encountered it.
Wez Furlong asked:
If you want to do this kind of thing, why not do it properly?
- Code: Select all
$foo = new $scanning_class;
$foo->scanBuffer($input);
that is, after all, what extends is all about.
As Jochem Mass noted, 'hihi, that why Wez get the 'King' prefix and everyone doesn't :-)'

