【问题标题】:How PHP file will know and process a function through this callback parameter?PHP文件如何通过这个回调参数知道和处理一个函数?
【发布时间】:2019-11-16 13:32:46
【问题描述】:

我正在寻找从 PHP 文件中获取数据 JSON 的方法,而不知道 PHP 文件正在处理的函数。

<script>
function clickButton(){
    var s = document.createElement("script");
    s.src = "jsonp_demo_db.php?callback=myDisplayFunction";
    document.body.appendChild(s);
}
</script>

【问题讨论】:

标签: php jsonp


【解决方案1】:

用get请求发送函数名并检查Get值是否存在 并使用call_user_func($functionName),如果有功能参数,您可以使用call_user_func_array($functionName , $pramaters)

`function test()
{
  echo "string";
}

if ($_GET['func']) {
  $func = $_GET['func'];
  call_user_func($func);
}`

您还可以使用 function_exists 来确保函数是否存在并且该函数返回 False 或 True 此代码示例:

if(function_exists('my_function'))
{
throw new Exception("'my_function' is already defined!");
}

function my_function()
{
  // Do the work here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-07
    • 2020-11-11
    • 2015-04-29
    • 2016-06-19
    • 2015-11-02
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    相关资源
    最近更新 更多