2017-03-27 45 views
1

我正在寻找一种方式来通过一个函数调用的函数,得到的功能等它的内容执行:Ajax响应

function response($execute){ 
    ob_clean(); 
    $execute(); 
    die(); 
} 

所以,当我打电话,我想给它一个参数的过程,如:

response(echo("hi")); 
+0

你期待'hi'功能应该叫什么名字? –

+0

@SahilGulati我想获得回声函数作为参数 –

+0

你是否只想要回声函数作为参数或任何'匿名'函数或闭包? –

回答

1

PHP使用匿名函数。

PHP code demo

function response($execute) 
{ 
    if(is_callable($execute)) 
    { 
     $execute("some-value"); 
    } 
    else 
    { 
     echo "Not a function"; 
    } 
} 

response(function($someVariable){ 
    echo "Hi i am in anonymous function with 1st argument ".$someVariable; 
}); 
response("Hi"); 
+0

谢谢高手! –

+0

@AmirForsatiQ。欢迎..... :) –

+0

有无论如何检测到$ execute是函数还是变量里面的response()? –