Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class variables holding a function in PHP

PHP allows for variables to hold functions like so:

$f = function($a,$b) {
   print "$a $b";
};
$f("Hello","World!"); //prints 'Hello World!'

This works just fine for me. I'm trying to pass a function into a class and set an instance variable to hold that function but with little luck:

class Clusterer {
    private $distanceFunc;
    public function __construct($f) {
        $this->distanceFunc = $f;
        print $f(1,7); //works
        print $this->distanceFunc(1,7); //exceptions and errors abound
    }
}
$func = function($a,$b) {
    return abs($a-$b);
}
$c = new Clusterer($func);

Am I doing something wrong here? The error is that the function doesn't exist so my guess currently is that it looks for a class function with that name (which there isn't one) and then gives up rather than looking for variables as well... how can I make it view the $this->distanceFunc as a variable?

EDIT: So after the advice from the answers below, I found a solution which was the make a function to wrap the invocation. For example my class is now:

class Clusterer {
    private $distanceFunc;
    public function __construct($f) {
        $this->distanceFunc = $f;
        print $f(1,7); //works
        print $this->distanceFunc(1,7); //exceptions and errors abound
    }
    private function distanceFunc($a,$b) {
        $holder = $this->distanceFunc;
        return $holder($a,$b);
    }
}
$func = function($a,$b) {
    return abs($a-$b);
}
$c = new Clusterer($func);

and this works great. Php looks for functions first and can only tell if it is a variable by context I guess is the moral of this story.

like image 817
hackartist Avatar asked Aug 08 '14 21:08

hackartist


People also ask

Can you assign a function to a variable in PHP?

PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it.

How do you call a function variable in PHP?

There are two methods for doing this. One is directly calling function by variable name using bracket and parameters and the other is by using call_user_func() Function but in both method variable name is to be used. call_user_func( $var );


1 Answers

Your code doesn't work because PHP interprets $this->distanceFunc(1,7) as a class method, but you can do the following:

class Clusterer {

    private $distanceFunc;

    public function __construct($f) {
        $this->distanceFunc = $f;
        print $f(1,7); //works
        print call_user_func_array($this->distanceFunc, array(1, 7));
//      print $this->distanceFunc(1,7); //exceptions and errors abound
    }
}
$func = function($a,$b) {
    return abs($a-$b);
};
$c = new Clusterer($func);

http://sandbox.onlinephpfunctions.com/code/cdc1bd6bd50f62d5c88631387ac9543368069310

like image 85
spacebiker Avatar answered Oct 02 '22 13:10

spacebiker