Problem With Using Anonymous Functions

Dear all,

I have encountered a problem on using anonymous function as a callback in Yii. Briefly speaking, I have a piece of code in the "component" folder that looks like:




class MyClass {

  public function test() {

    $array = array( array('a', 1), array('b', 2) );

    $myComparator = function ($a, $<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />  {

	if ($a[1] == $b[1]) return 0; else if ($a[1] > $b[1]) return -1; else return 1; 

    };	

    usort($array, $myComparator);	

    return $array;

  }

}



When I accessed this method from a standard controller, in which I just var_dump-ed the return value to the browser window, I receive no output at all. But if I accessed it without touching Yii framework (such as invoking it in a terminal) I obtained the expected sorted array. There was no error message both cases.

I couldn’t figure out why in my Yii environment the anonymous function doesn’t work right. I have searched in the forum, and noticed that the only relevant post to this issue said “The current framework is compatible with PHP 5.2 and so can’t use anonymous functions. 2.0 will be PHP 5.3+ though.”. If this is correct, may someone clarify with a official link please? Or may anyone leave me some hints about the problem?

Many thanks in advance!

P.S. I am using Yii-1.1.8 with PHP 5.3.17.

These are my advises below.

First step,you should checkout the yii controller output source code to ensure that whether the output has been treated as invalid html content by web browser,and result in what you seen:receive no output at all.

(Also check that,if any error message printed)

You can use this command on linux.

$ curl -i http://your-app-host/your-controller-url/

Then,check your php.ini file,ensure set ‘display_errors=On’,so if your php version doesn’t support anonymous function,you can see an error in output.

PHP 5.3.17. support anonymous function.So use it in yii framework has no problem.

I think your problem is not caused by anonymous function’s usage;

It is due to eaccelerator.

Disable this PHP module, the problem should be solved.