Re: Array pointer to a function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Apr 8, 2008 at 12:00 AM, hce <webmail.hce@xxxxxxxxx> wrote:

> Hi,
>
> Is it possible for an array to point a function:
>
> Asignmanet
>
> $ArrayPointer = $this->MyFunction;
>
> Invoke:
>
> $ArraryPointer();


i would recommend you investigate variable functions

1.  http://www.php.net/manual/en/functions.variable-functions.php

so you dont exactly get functional language behavior; but you can 'pass
around a function'.  so for example; if you have
<?php
function someFunc() {}
/**
 * then you can pass around strings that refer to it; as in
 */
$someFuncPointer = 'someFunc';

/**
 * then you can call it using the variable function construct; as in
 */
$someFuncPointer();  // invoking someFunc()
?>

you can pass parameters to such an invocation; as in
<?php
$someFuncPointer(1, $b, $yaddaYadda);
?>

and it works for object instances
<?php
class MyClass { function doStuff() {} }
$b = new MyClass();
$b->doStuff();
?>

and it works for static object method calls
<?php
class OtherClass { static function doMoreStuff() {} }
$b = 'doMoreStuff';
?>

you can also store the class name or instance in variables and it still
works.  here the output of a little php -a session

<?php
class M { static function b() {} function n() {} }
$m = 'M';
$b = 'b';
$m::$b();
$n = new M();
$n->b();
$n->$b();
?>

and thats just the tip of the iceberg!  there is also the php psuedo type
callback; and there are some other functions that are pertinent.
call_user_func();  // very similar to variable functions
call_user_func_array()

-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux