|
Posted by Rik on 08/16/07 15:08
On Thu, 16 Aug 2007 17:00:54 +0200, D_a_n_i_e_l <danwgrace@gmail.com> =
wrote:
> Is there some way to implement something like a virtual function or
> have something like a user defined function ? One solution I thought
> might work: store a reference (address) of a function in a variable
> and call it later if that variable has been set ? This seems to
> compile OK, but produces funny results. Is this outside the scope of
> php ?
>
> function testfn()
> {
> ...
> return $result;
> }
>
> $myfn =3D &testfn; // set it too the address of the function
> ...
> if (isset($myfn))
> $result =3D @$myfn(); // call my function
Just make the variable a string with the function name.
<?php
function testfn(){echo 'foo';}
$myfn =3D 'testfn';
//...
if(isset($myfn)) $myfn();
?>
You can also use anonymous functions (without explicitly giving a name, =
=
allthough they do have one...) with create_function():
<?php
$func =3D create_function('$var','echo strtoupper($var);');
//option 1 to call it:
$func('foo');
//option 2 to call it:
call_user_func($func,'bar');
?>
-- =
Rik Wasmus
Navigation:
[Reply to this message]
|