Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a parameter to a callable function

Tags:

php

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

like image 770
myol Avatar asked Feb 23 '16 11:02

myol


People also ask

Can we pass parameter in callable?

The first way we can send a parameter to a thread is simply providing it to our Runnable or Callable in their constructor.

What does the callable function do in Python?

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.

What is a callable argument?

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.

What does it mean for a function to be callable?

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!


2 Answers

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 ) );

Edit:

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 );
like image 142
fusion3k Avatar answered Oct 22 '22 17:10

fusion3k


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

like image 36
DonCallisto Avatar answered Oct 22 '22 17:10

DonCallisto