Accept function as parameter in PHP

后端 未结 8 771
Happy的楠姐
Happy的楠姐 2020-11-28 06:22

I\'ve been wondering whether is possible or not to pass a function as parameter in PHP; I want something like when you\'re programming in JS:

object.exampleM         


        
8条回答
  •  野性不改
    2020-11-28 06:53

    Tested for PHP 5.3

    As i see here, Anonymous Function could help you: http://php.net/manual/en/functions.anonymous.php

    What you'll probably need and it's not said before it's how to pass a function without wrapping it inside a on-the-fly-created function. As you'll see later, you'll need to pass the function's name written in a string as a parameter, check its "callability" and then call it.

    The function to do check:

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

    Then, to call it, use this piece of code (if you need parameters also, put them on an array), seen at : http://php.net/manual/en/function.call-user-func.php

    call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
    

    as it follows (in a similar, parameterless, way):

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

    Here's a class explaining how to do something similar :

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

    and an example of usage

    
             
    hello to
    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)*/ ?>

提交回复
热议问题