I would like to have the functionality to set a function in a class (function setFunction
). Later I want to get the function (with getFunction
), which I can call from outside (specifying parameters etc. ). Here is what I have tried:
class Test
{
var $function;
function setFunction($foo)
{
$this->function = $foo;
}
function getFunction()
{
return $this->function;
}
function foo1($a)
{
print "foo1: ".$a."\n";
}
function foo2($b)
{
print "foo2: ".$b."\n";
}
}
$oClass = new Test();
$oClass->setFunction($oClass->foo1);
$oClass->getFunction()('test'); # <--- line 32
The expected output is
foo1: test
But I get an error instead:
PHP Parse error: syntax error, unexpected '(' in tester.php on line 32
Any ideas how to solve this? Here are some constraints:
- This has to work for arbitrary parameters for each function.
- The function to be defined is always specified in the class itself (if that helps).