5

it is possible to assign to a class variable a function at runtime to be executed? a kind of "function pointer" like C

something like this: (this won't work because sum is out of the scope of A, but this is the pattern i mean)

 class A {
    public $function_name;
    public functon run($arg1,$arg2){
        $function_name($arg1,$arg2);
    }
}  

function sum($a,$b){
    echo $a+$b;
}

$a=new A();
$a->function_name='sum';

$a->run();     

[edit] i know there is "call_user_func" but it need as i understand to have the function in the scope or use a public class method

Zorb
  • 726
  • 11
  • 24
  • possible duplicate of [How to add a new method to a php object on the fly?](http://stackoverflow.com/questions/2938004/how-to-add-a-new-method-to-a-php-object-on-the-fly) – mario Dec 16 '11 at 18:16

5 Answers5

3

You could use an anonymous function if you use PHP >5.3.0:

$sum = function($a, $b) {
    return $a+$b;
}

$a->function_name = $sum;
Eric
  • 1,356
  • 14
  • 24
2

Using call_user_func_array:

<?php
class A {

    public $function_name;

    public function run($arg1,$arg2){

        return call_user_func_array( $this->function_name, array($arg1, $arg2 ) );

    }
}  

function sum($a,$b){
    return $a+$b;
}

$a=new A();
$a->function_name= 'sum';

var_dump( $a->run(1,1) ); //2

?>
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • Cool! but i need that the function declaration and the the class initialization are in the same scope? – Zorb Dec 16 '11 at 18:12
  • @Zorb, what do you mean, sum is a public method of class A ? Can you edit your original question and put sum where it's supposed to be – Esailija Dec 16 '11 at 18:15
  • You're kind of misusing `call_user_func_array()` here ... if you know the parameters you should simply use `call_user_func()`, if not, you should do `$args = func_get_args(); call_user_func_array($this->function_name, $args);` instead of creating an array from the known parameters. –  Dec 16 '11 at 18:17
  • @rdlowrey sure, don't know why it popped into my head first. Just wanted to get a fast answer out :P – Esailija Dec 16 '11 at 18:21
  • @Esailija i thinking about if the class cant see directly the function and i pass it the function can the class use it? now is clear , thanks! – Zorb Dec 16 '11 at 18:24
  • @Esailija haha lol on the speed thing. Gotta get 'em answered before the SO sharks start circling :) –  Dec 16 '11 at 18:25
1

It works regardless of scope. You just gotta call it using call_user_func. I also fixed a couple of typos in your example.

<?php
    class A {
        public $function_name;
        public function run($arg1, $arg2) {
        call_user_func($this->function_name, $arg1, $arg2);
        }
    }  

    function sum($a, $b){
        echo $a + $b;
    }

    $a = new A();
    $a->function_name = 'sum';

    $a->run(2, 3);
?>

Live example

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
0

Another way is to make use variable variables (applicable to object method)

public static function sum($arg1, $arg2)
{
  ..
}

public function run($arg1, $arg2)
{
  $func = $this->function_name;
  $func( $arg1, $arg2);         <-- procedural call
  self::$func($arg1, $arg2);    <-- static method call
}
ajreal
  • 46,720
  • 11
  • 89
  • 119
0

Use any variation of the Callback pseudo type. Use it with call_user_func or call_user_func_array

The manual gives great examples of usage for the above.

Also see the new php 5.4 Closure::bindTO method if you want to be able to easily use the $this keyword in it.

goat
  • 31,486
  • 7
  • 73
  • 96