|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
Is it possible for an array to point a function: Asignmanet $ArrayPointer = $this->MyFunction; Invoke: $ArraryPointer(); |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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/functio...-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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Tue, 2008-04-08 at 14:00 +1000, hce wrote: > Hi, > > Is it possible for an array to point a function: > > Asignmanet > > $ArrayPointer = $this->MyFunction; > > Invoke: > > $ArraryPointer(); No, you can't do what you've done above. What you can do is... <?php $ptr = array( 'obj' => &$this, 'method' => 'MyFunction' ); $ptr['obj']->$ptr['method'](); ?> Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP |
|
![]() |
| Outils de la discussion | |
|
|