You are here: Re: address of function or a virtual function « PHP Programming Language « IT news, forums, messages
Re: address of function or a virtual function

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация