Re: Array pointer to a function
| From: | Nathan Nobbe | Date: | Tue, 08 Apr 2008 04:55:05 +0000 |
| Subject: | Re: Array pointer to a function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-272682@lists.php.net to get a copy of this message | ||
On Tue, Apr 8, 2008 at 12:00 AM, hce <webmail.hce@gmail.com> 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