Accetta la funzione come parametro in PHP


104

Mi chiedevo se sia possibile o meno passare una funzione come parametro in PHP; Voglio qualcosa come quando programmi in JS:

object.exampleMethod(function(){
    // some stuff to execute
});

Quello che voglio è eseguire quella funzione da qualche parte in exampleMethod. È possibile in PHP?


Risposte:


150

È possibile se stai usando PHP 5.3.0 o versioni successive.

Vedere Funzioni anonime nel manuale.

Nel tuo caso, definiresti exampleMethodcosì:

function exampleMethod($anonFunc) {
    //execute anonymous function
    $anonFunc();
}

9
Dannazione, sapevo che era possibile. Stavo per rispondere, ma prima volevo trovare della documentazione a cui collegarmi e non sapevo come si chiamasse esattamente. Ah beh, ora saprò per quando dovrò farlo anche io. Grazie.
Rob il

3
Prima della 5.3 è possibile utilizzarecreate_function
Gordon

Grazie mille. Dato che devo farlo per PHP4.3 immagino che dovrò fare quello che voglio usando un'altra logica.
Cristian

1
@Casidiablo - Vedi la risposta di Jage, potresti fare qualcosa con create_function(). Non è proprio la stessa cosa, poiché devi passare la tua funzione come una stringa di codice, che poi viene messa eval()dietro le quinte. Non è l'ideale, ma potresti essere in grado di usarlo.
zombat

1
Come lo chiameresti? Potreste dare un nome alla funzione esistente? Ad esempio:exampleMethod(strpos);
sumid

51

Solo per aggiungere agli altri, puoi passare un nome di funzione:

function someFunc($a)
{
    echo $a;
}

function callFunc($name)
{
    $name('funky!');
}

callFunc('someFunc');

Funzionerà in PHP4.


17

Puoi anche usare create_function per creare una funzione come variabile e passarla in giro. Tuttavia, mi piace di più la sensazione di funzioni anonime . Vai a zombat.


+1 per l'alternativa. Sembra che l'OP potrebbe averne bisogno.
zombat

Grazie! Devo restare con una vecchia installazione di PHP 5.2 e le funzioni di anonymoys non funzionano lì.
diosney

Attenzione: questa funzione è stata DEPRECATA a partire da PHP 7.2.0. Affidarsi a questa funzione è altamente sconsigliato.
Shamaseen

15

Basta codificarlo in questo modo:

function example($anon) {
  $anon();
}

example(function(){
  // some codes here
});

sarebbe fantastico se potessi inventare qualcosa di simile (ispirato a Laravel Illuminate):

Object::method("param_1", function($param){
  $param->something();
});

esattamente quello che stavo cercando
Harvey

5

VERSIONE PHP> = 5.3.0

Esempio 1: base

function test($test_param, $my_function) {
    return $my_function($test_param);
}

test("param", function($param) {
    echo $param;
}); //will echo "param"

Esempio 2: oggetto std

$obj = new stdClass();
$obj->test = function ($test_param, $my_function) {
    return $my_function($test_param);
};

$test = $obj->test;
$test("param", function($param) {
    echo $param;
});

Esempio 3: chiamata di classe non statica

class obj{
    public function test($test_param, $my_function) {
        return $my_function($test_param);
    }
}

$obj = new obj();
$obj->test("param", function($param) {
    echo $param;
});

Esempio 4: chiamata di classe statica

class obj {
    public static function test($test_param, $my_function) {
        return $my_function($test_param);
    }
}

obj::test("param", function($param) {
    echo $param;
});

5

Secondo la risposta di @ zombat, è meglio convalidare prima le funzioni anonime:

function exampleMethod($anonFunc) {
    //execute anonymous function
    if (is_callable($anonFunc)) {
        $anonFunc();
    }
}

Oppure convalida il tipo di argomento a partire da PHP 5.4.0:

function exampleMethod(callable $anonFunc) {}

3

Testato per PHP 5.3

Come vedo qui, Anonymous Function potrebbe aiutarti: http://php.net/manual/en/functions.anonymous.php

Quello che probabilmente ti servirà e non è detto prima è come passare una funzione senza avvolgerla in una funzione creata al volo . Come vedrai in seguito, dovrai passare il nome della funzione scritto in una stringa come parametro, verificarne la "chiamabilità" e quindi chiamarla.

La funzione da controllare:

if( is_callable( $string_function_name ) ){
    /*perform the call*/
}

Quindi, per chiamarlo, usa questo pezzo di codice (se hai bisogno anche di parametri, inseriscili su un array), visto su: http://php.net/manual/en/function.call-user-func.php

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );

come segue (in modo simile, senza parametri):

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");

Ecco una lezione che spiega come fare qualcosa di simile:

<?php
class HolderValuesOrFunctionsAsString{
    private $functions = array();
    private $vars = array();

    function __set($name,$data){
        if(is_callable($data))
            $this->functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>

e un esempio di utilizzo

<?php
    /*create a sample function*/
    function sayHello($some = "all"){
    ?>
         <br>hello to <?=$some?><br>
    <?php
    }

    $obj = new HolderValuesOrFunctionsAsString;

    /*do the assignement*/
    $obj->justPrintSomething = 'sayHello'; /*note that the given
        "sayHello" it's a string ! */

    /*now call it*/
    $obj->justPrintSomething(); /*will print: "hello to all" and
        a break-line, for html purpose*/

    /*if the string assigned is not denoting a defined method
         , it's treat as a simple value*/
    $obj->justPrintSomething = 'thisFunctionJustNotExistsLOL';

    echo $obj->justPrintSomething; /*what do you expect to print?
        just that string*/
    /*N.B.: "justPrintSomething" is treated as a variable now!
        as the __set 's override specify"*/

    /*after the assignement, the what is the function's destiny assigned before ? It still works, because it's held on a different array*/
     $obj->justPrintSomething("Jack Sparrow");


     /*You can use that "variable", ie "justPrintSomething", in both ways !! so you can call "justPrintSomething" passing itself as a parameter*/

     $obj->justPrintSomething( $obj->justPrintSomething );
         /*prints: "hello to thisFunctionJustNotExistsLOL" and a break-line*/

    /*in fact, "justPrintSomething" it's a name used to identify both
         a value (into the dictionary of values) or a function-name
         (into the dictionary of functions)*/
?>

2

Semplice esempio utilizzando una classe:

class test {

    public function works($other_parameter, $function_as_parameter)
    {

        return $function_as_parameter($other_parameter) ;

    }

}

$obj = new test() ;

echo $obj->works('working well',function($other_parameter){


    return $other_parameter;


});
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.