PHP-了解create_function()-传递简单变量 [英] Php - understanding create_function() - passing simple variable

查看:128
本文介绍了PHP-了解create_function()-传递简单变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用动态create_function,到目前为止,还没有太大的成功:-)

First time I am trying to use the dynamic create_function, and up to now, not much success :-)

我的功能是这样的:

 function o99_brsa_custom_widgets() {
        global $wp_meta_boxes;
        global $o99_brsa_options;

        for($i=0; $i> count($o99_brsa_options[content]); $i++) {

            $widgt_id = 'o99_dashboard_widget_dyn' . $i;
            $widgt_name = 'obmek99 widget name' . $i;
            $out = $o99_brsa_options[content][$i];
            $f = create_function(' $out ',' global $out; echo $out;');
            do_the_widgets($widgt_id, $widgt_name, $f);
         }
    } 

do_the_widgets()操作仅接受直接回显并打印窗口小部件的内容.

The do_the_widgets() action is accepting only a direct echo and prints the content of the widget.

$o99_brsa_options[content]是具有$i个元素(每个都是内容)的经过验证的数组.

The $o99_brsa_options[content] is a verified array with $i elements (each is content) .

奇怪的是,$i$widgt_id$widgt_name上都起作用,但是在create_function()上,我在所有小部件上都打印了相同的值. (echo $out)

The strange thing is that the $i is working on the $widgt_id and $widgt_name but on the create_function() I get the same value printed in all widgets . ( echo $out )

似乎我不知道如何将一个简单的变量传递给新函数(我正在create_function()中使用global,但是到目前为止,它的作用不大.

It seems that I do not know how to pass a simple variable to the new function ( I am using global inside create_function(), but it helps little as for now .

那么,我现在的错误/误解/误用是什么:-) ??

So, what is my mistake / misunderstanding / misuse now :-) ??

推荐答案

create_functionstone age期间,当

create_function was during the stone age , when kaᵠ used pen and paper to write applications, when PeeHaa埽 got a beer because he wrote hello would, The world is better now please use closures

$f = function ($out) {
    echo $out;
};

$f("Welcome");

有一天我要感谢我,但是如果您是 Gordon (过去寄来的机器在这里折磨我们),他是这样写的

You would thank me one day, But you can only use create_function if you are Gordon (The machine from the past sent here to torment us) he wrote this

$fn = create_function(
    '$x',
    'return $x; } $foo = 42; function foo($int) { return $int; '
);

参见实时演示

这篇关于PHP-了解create_function()-传递简单变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆