address of function or a virtual function

address of function or a virtual function

am 16.08.2007 17:00:54 von D_a_n_i_e_l

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 = &testfn; // set it too the address of the function
....
if (isset($myfn))
$result = @$myfn(); // call my function

Re: address of function or a virtual function

am 16.08.2007 17:04:03 von zeldorblat

On Aug 16, 11:00 am, D_a_n_i_e_l 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 = &testfn; // set it too the address of the function
> ...
> if (isset($myfn))
> $result = @$myfn(); // call my function

You can find out if a function exists using function_exists():

if(function_exists('testfn')
...

You can also call a function who's name is stored in a variable:

$myfn = 'testfn';
$result = $myfn(); //calls testfn()

Is that what you're trying to do?

Re: address of function or a virtual function

am 16.08.2007 17:08:06 von luiheidsgoeroe

On Thu, 16 Aug 2007 17:00:54 +0200, D_a_n_i_e_l =

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.

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

$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

Re: address of function or a virtual function

am 16.08.2007 17:09:20 von D_a_n_i_e_l

On 16 Aug, 16:04, ZeldorBlat wrote:
>
> You can find out if a function exists using function_exists():
>
> if(function_exists('testfn')
> ...
>
> You can also call a function who's name is stored in a variable:
>
> $myfn = 'testfn';
> $result = $myfn(); //calls testfn()
>
> Is that what you're trying to do?- Hide quoted text -
>
> - Show quoted text -

Yes, thank you very much.
PHP is suprisingly powerful.

Re: address of function or a virtual function

am 16.08.2007 17:41:34 von ELINTPimp


> PHP is suprisingly powerful.

http://www.php.net/usage.php

I'm not too surprised.