|
Posted by Egbert Teeselink on June 1, 2008, 10:27 am
Please log in for more thread options
> $stdObject->sayHi =3D create_function('', 'print "Hi";');
>
> How to call sayHi without do something like:
> $sayHi =3D & $stdObject->sayHi;
> $sayHi();
>
> Any good idears?or no idear.
An option is call_user_func($stdObject->sayHi);
It's important to recognise that $stdObject->sayHi simply contains a
string with the new function's name. As such, you don't need the
reference,
$sayHi =3D $stdObject->sayHi;
$sayHi();
will also work. The reason "$stdObject->sayHi();" doesn't work is
syntactical; PHP thinks sayHi is a method of $stdObject, and "variable
functions" only work on literal variables, not on expressions (such as
$stdObject->sayHi) - http://www.php.net/manual/en/functions.variable-functio=
ns.php
-egbert
|