Real coders do it with PHP.

Bar discussions . Talk about everything not only php !. Guests are welcome here , forum registration is not required.

Moderators: macek, egami, gesf

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Real coders do it with PHP.

Postby Alexej Kubarev » Mon Jan 24, 2005 10:26 am

I thought this might be quite ammusing to read for some of us who understand it :)

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 :-)'

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Sun Jun 12, 2005 2:33 am

Dmn..i ve got really bad memory as i jsut read this post of my own as a new one to me ^^ :) still something to think about.. once again :)


Return to “Free time”

Who is online

Users browsing this forum: No registered users and 1 guest