5

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

My function is this :

 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);
         }
    } 

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

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

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 )

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 :-) ??

Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • If `$out` is going to be a global then what are you passing it as an argument? – prodigitalson May 06 '13 at 14:52
  • Because somehow , if i don´t , nothing get printed .. – Obmerk Kronen May 06 '13 at 14:54
  • 1
    Well if youre going to use it as a global then its value would already be configured outside the function, and then you would modify it inside (or not) and that modification would then apply globally not just within. If you just need to pass in an argument then remove the global. If you need to do both change the name of the argument and then have it modify `$out` inside the function. Example: `create_function('$arg', 'global $out; echo $out . $arg;');` – prodigitalson May 06 '13 at 14:57

1 Answers1

19

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");

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; '
);

See Live Demo

Community
  • 1
  • 1
Baba
  • 94,024
  • 28
  • 166
  • 217
  • 1
    :-) I am thanking you already now :-) . but what to do if I need support for PHP 5.2.9? – Obmerk Kronen May 06 '13 at 15:01
  • 1
    Do you how how many bugs are in ` PHP 5.2.9` ? I think you should upgrade right away .... because if if you get this working more problems would follow – Baba May 06 '13 at 15:04
  • Yes sir ! :-) you are actually right . Thanks for the tip (I did not know about closure, i guess the tools ARE important for progress :-) . And your links are very cool ( especially the 99 beers one ) – Obmerk Kronen May 06 '13 at 15:05
  • You are welcome ... here are more tips http://php.net/manual/en/functions.anonymous.php and http://stackoverflow.com/questions/2412299/how-do-you-use-anonymous-functions-in-php – Baba May 06 '13 at 15:09
  • ok. so I guess i need to change the title from 'understanding create_function() ' to `Should I use create_function() ??` - answers would be much shorter then.. – Obmerk Kronen May 06 '13 at 15:20
  • Just use [anonymous function](http://php.net/manual/en/functions.anonymous.php) .. they are much easy to understand .. i only added the `create_function` example there for you to see how complicated it can be – Baba May 06 '13 at 15:29