Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a function name as parameter in a function

Tags:

php

I'm having a function like this:

function test($str,$func) {
    $func($str);
    //Something more here
}

How can I pass a function name there? I want to do like test("Hello",strtolower); or test("Bye",sometextprocessfunc);

like image 434
potasmic Avatar asked Jan 05 '14 14:01

potasmic


3 Answers

function test($str, $func){
    return call_user_func($func,$str);
} 

Use it like this

$asd = test("ASD","strtolower");
like image 105
Jompper Avatar answered Oct 13 '22 11:10

Jompper


As any other string,

test("Hello","strtolower");

See variable functions

like image 43
Maxim Krizhanovsky Avatar answered Oct 13 '22 11:10

Maxim Krizhanovsky


If you want to know all the possibilities, take a look at the callable types documentation

Callback Types

Example #1 Callback function examples

<?php 

// An example callback function
function my_callback_function() {
    echo 'hello world!';
}

// An example callback method
class MyClass {
    static function myCallbackMethod() {
        echo 'Hello World!';
    }
}

// Type 1: Simple callback
call_user_func('my_callback_function'); 

// Type 2: Static class method call
call_user_func(array('MyClass', 'myCallbackMethod')); 

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));

// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');

// Type 5: Relative static class method call (As of PHP 5.3.0)
class A {
    public static function who() {
        echo "A\n";
    }
}

class B extends A {
    public static function who() {
        echo "B\n";
    }
}

call_user_func(array('B', 'parent::who')); // A
?>

Example #2 Callback example using a Closure

<?php
// Our closure
$double = function($a) {
    return $a * 2;
};

// This is our range of numbers
$numbers = range(1, 5);

// Use the closure as a callback here to 
// double the size of each element in our 
// range
$new_numbers = array_map($double, $numbers);

print implode(' ', $new_numbers);
?>

And also, take a look at the variable functions, that other people already told you about

Variable functions

Example #1 Variable function example

<?php
function foo() {
    echo "In foo()<br />\n";
}

function bar($arg = '')
{
    echo "In bar(); argument was '$arg'.<br />\n";
}

// This is a wrapper function around echo
function echoit($string)
{
    echo $string;
}

$func = 'foo';
$func();        // This calls foo()

$func = 'bar';
$func('test');  // This calls bar()

$func = 'echoit';
$func('test');  // This calls echoit()
?>

Object methods can also be called with the variable functions syntax.

Example #2 Variable method example

<?php
class Foo
{
    function Variable()
    {
        $name = 'Bar';
        $this->$name(); // This calls the Bar() method
    }

    function Bar()
    {
        echo "This is Bar";
    }
}

$foo = new Foo();
$funcname = "Variable";
$foo->$funcname();  // This calls $foo->Variable()

?>

When calling static methods, the function call is stronger than the static property operator:

Example #3 Variable method example with static properties

<?php
class Foo
{
    static $variable = 'static property';
    static function Variable()
    {
        echo 'Method Variable called';
    }
}

echo Foo::$variable; // This prints 'static property'. It does need a $variable in this scope.
$variable = "Variable";
Foo::$variable();  // This calls $foo->Variable() reading $variable in this scope.

?>
like image 36
Carlos Robles Avatar answered Oct 13 '22 12:10

Carlos Robles