I can't seem to get this to work. I have a function which takes a parameter which I would like to call.
protected function testFunc($param) {
echo $param;
}
protected function testCall(callable $testFunc) {
call_user_func($testFunc);
}
public function testProg($param) {
$this->testCall([$this, 'testFunc']);
}
I have tried
$this->testCall([[$this, 'testFunc'], $param]);
and
$this->testCall([$this, 'testFunc($param)']);
and
$this->testCall('TestClass::testFunc($param));
Are closures my only option here or how can I pass a parameter to a callable function
The first way we can send a parameter to a thread is simply providing it to our Runnable or Callable in their constructor.
callable() in Python This built-in method in Python checks and returns True if the object passed appears to be callable, but may not be, otherwise False. The callable() method takes only one argument, an object and returns one of the two values: returns True, if the object appears to be callable.
callable() is a built-in method in Python that checks if a passed object appears to be callable. To do this, it checks if the argument is either of the following: The type that demonstrates callability, such as a function or a method. An instance of a class with a __call__ method.
In Python, a callable is a function-like object, meaning it's something that behaves like a function. Just like with a function, you can use parentheses to call a callable. Functions are callables in Python but classes are callables too!
To call a method (in your example function
(s) are class methods), you have to use this syntax:
protected function testCall( $testFunc )
{
call_user_func( array( $this, $testFunc ) );
}
To pass an argument, you have to use this syntax:
protected function testCall( $testFunc, $arg )
{
call_user_func( array( $this, $testFunc ), $arg );
}
(...)
$this->testCall( 'testFunc', $arg );
To pass more than one arguments, you have to use call_user_func_array
:
protected function testCall( $testFunc, array $args )
{
call_user_func_array( array( $this, $testFunc ), $args );
}
(...)
$this->testCall( 'testFunc', array( $arg1, $arg2 ) );
The above code works fine, but — as quick-wittedly noted in comments — this previous code:
protected function testCall( callable $testFunc, $arg )
doesn't work in above context.
To use it, the above methods and call must be modified in:
protected function testCall( callable $testFunc, $arg )
{
call_user_func( $testFunc , $arg );
}
(...)
$this->testCall( array( $this, 'testFunc'), $arg );
Modify your code as follows
protected function testFunc($param) {
echo $param;
}
protected function testCall(callable $testFunc, $param) {
call_user_func($testFunc, $param);
}
public function testProg($param) {
$this->testCall([$this, 'testFunc'], $param);
}
That way, first function (testCall
) is accepting arguments for second function (testFunc
).
This code works only if you're passing a single argument. If you wish to pass an array, use call_user_func_array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With