Sorting Object Members

So there I was, trying to sort object internal data with a user-defined function and going nowhere, until it hit me: I could simply promote the data to the global scope using $GLOBALS and then import it inside the function. It even works for lambda-style functions created with create_function:

    $GLOBALS['gOPMLData'] = $this->m_aData; // promote internal data to global scope    foreach( $this->m_aLetters as $szLetter => $aKeys ) {      usort( $aKeys, create_function('$a,$b','        global $gOPMLData;        $a = strtolower($gOPMLData[$a]["TITLE"]);        $b = strtolower($gOPMLData[$b]["TITLE"]);        if( $a == $b )           return 0;        return ( $a < $b ) ? -1 : 1;' ));      $this->m_aLetters[$szLetter] = $aKeys;    }    unset( $GLOBALS['gOPMLData'] ); // clean up global scope

The reason I ended up doing this is that trying to pass a member function to usort (usort($aFoo,'$this->mysort')) doesn't work in the current builds of PHP. The lambda function is just to make things more compact (and not having to look for a stray function when changing the class behaviour).


See Also

PHP