【问题标题】:Turn anonymous function into a normal function将匿名函数变成普通函数
【发布时间】:2013-11-18 16:03:05
【问题描述】:
    add_action( 'wp_footer', function () use($uniqueid, $height, $tileheight, $margin) {
    echo "<script type='text/javascript'>
            jQuery(function () {
                jQuery('." . $uniqueid . "').tilesGallery({
                    responsive: true,
                    anchorVertical: 'middle',
                    anchorHorizontal: 'center',
                    reloadOnResize: true,
                    captionHeight: '100%',
                    height: ".$height.",
                    tileMinHeight: ".$tileheight.",                     
                    margin: ".$margin.",
                });
            });
        </script>";
});

你好,

问题是我不能对低于 5.3 的 php 版本使用这个功能。我怎样才能把它变成可以在低于 5.3 的 php 版本中运行的东西?

提前谢谢你!

【问题讨论】:

    标签: anonymous-function


    【解决方案1】:

    只需使用 create_function($args, $code);
    这里如何使用create_function()

    $function = create_function('', 
    '
    global $uniqueid, $height, $tileheight, $margin;
        echo "<script type=\"text/javascript\">
                jQuery(function () {
                    jQuery(\'.$uniqueid\').tilesGallery({
                        responsive: true,
                        anchorVertical: \'middle\',
                        anchorHorizontal: \'center\',
                        reloadOnResize: true,
                        captionHeight: \'100%\',
                        height: $height,
                        tileMinHeight: $tileheight,                     
                        margin: $margin,
                    });
                });
            </script>";
    '
    );
    

    【讨论】:

    • 我在末尾添加了缺少的括号,但还有其他错误:“解析错误:语法错误,意外的 T_STRING,在 /home/livetoth/public_html/wp-content 中需要 T_VARIABLE 或 '$' /themes/royal-mag/functions/visual-composer.php(1202) : 第 1 行运行时创建的函数
    • 感谢您花时间解决我的问题,但我没有看到两个代码之间的区别(旧代码和编辑代码,我尝试了单括号和双括号的不同组合但没有似乎有效?你能再看看编辑过的版本吗?
    • 我又编辑了,我只是把它放到一个var '$function'中并调整了'和",直接使用就行了
    • create_function,工作方式与基本代码类似,但将代码放入''或""
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多