Yii Framework Forum: Using Anonymous Function Doesn't Give Expected Result. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Using Anonymous Function Doesn't Give Expected Result. Rate Topic: -----

#1 User is offline   stephenLikesYii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 27-November 12

Posted 27 November 2012 - 11:45 PM

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, $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.
0

#2 User is offline   stephenLikesYii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 27-November 12

Posted 27 November 2012 - 11:51 PM

Sorry I have posted to the wrong section by mistake.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users