0

I am having this problem When using callback functions

Class My_Class {

     public function my_function() {

            $pad = function($value) {
            return str_pad($value, 2, '0', STR_PAD_LEFT);
            };

            function pad_function($value) {
                 return str_pad($value, 2, '0', STR_PAD_LEFT);
            }

            array_map($pad, range(0,100)); //This fails with an exception "Invalid opcode 153/1/8."
            array_map("pad_function", range(0,100)); //This works ok
        }



}

I am using PHP version 5.3.3-7.

Any ideas of why this is happening?

Thanks in advance!

hakre
  • 193,403
  • 52
  • 435
  • 836
Juan Antonio Gomez Moriano
  • 13,103
  • 10
  • 47
  • 65

2 Answers2

1

Finally the problem was with eaccelerator.

Version 1.0-dev of eaccelerator carashes when executing the code. Version 0.9.6.1 of eaccelerator does not crash with the code.

Juan Antonio Gomez Moriano
  • 13,103
  • 10
  • 47
  • 65
0

Typos apart, $pad is never defined.

Then array_map won't call your $my_pad_function lambda function, because you passed the my_pad_function string as first parameter, telling PHP to look for a function named my_pad_function: that's different from calling a lambda stored in $my_pad_function.

Anyway I advice you not to call everything "my_function", "myPrettyFunction", "myPointlessNameVar": give meaningful names even in playground code, your goal will be clearer.

One last thing:

lambdas : PHP = lipstick : pig
moonwave99
  • 21,957
  • 3
  • 43
  • 64
  • Thanks for the advices, the reason for calling them My_Whatever is because i didn't want to just copy & paste my own code (as it contains more vars and so on), that is also the reason why there were so many errors :). – Juan Antonio Gomez Moriano Aug 23 '12 at 04:46