PHP匿名函数链接

标签 php anonymous-function

$app = 'App here';


$fn1 = function($var) use($app){
    $fn2($var);
};

$fn2 = function($var) use($app){
    echo $var;
};

$fn1('variable');

在上面的例子中,我试图链接/转发多个匿名函数。但是,在下面的行中,我收到一条错误消息“注意: undefined variable :fn2”

$fn2($var)

如何实现匿名函数的链接。

最佳答案

问题是您没有将 $fn2 作为参数传递到闭包的 use 语句中。

试试下面的代码:

    $app = 'App here';

    $fn2 = function($var) use($app){
       echo $var;
    };

    $fn1 =  function($var) use($app, $fn2){
       $fn2($var);
    };

    $fn1('variable');

Here you have your example working in an online php tester.

关于PHP匿名函数链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31420389/

相关文章:

php - PHP 中的匿名函数性能

recursion - Julia:一起添加匿名函数

c# - 匿名方法如何省略参数列表?

javascript - 数据库兼容倒计时

php - 在不重新加载的情况下使用 textarea 内容更新 MySQL

javascript匿名函数语法变体

javascript - 在 JavaScript IF 语句中使用匿名函数

javascript - JQuery 仅重新加载 div 数据

php:是否有充分的理由引用所有数组键/索引?

php - OpenAI ChatGPT (GPT-3.5) API : How do I extract the message content from the response?