Problems while using a callback function in PHP

2019-08-22 16:57发布

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!

标签: php callback
2条回答
该账号已被封号
2楼-- · 2019-08-22 17:43

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
查看更多
Summer. ? 凉城
3楼-- · 2019-08-22 17:51

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.

查看更多
登录 后发表回答