PHP anonymous functions scope question

Posted by Dan on Stack Overflow See other posts from Stack Overflow or by Dan
Published on 2010-04-27T15:54:23Z Indexed on 2010/04/27 16:03 UTC
Read the original article Hit count: 313

Filed under:
|
|

Hi, I'm trying to sort an array of objects by a common property, however I cannot get my $property parameter to register in the inner function (I can use it in the outer one OK).

The way I read the documentation, it sounded like the parameter would be available, have I misunderstood something?

Here is what I have:

public static function sortObjectsByProperty($objects, $property)  
  {     
        function compare_object($a, $b) 
        {   
            $a = $a->$property;
            $b = $b->$property;

            if ($a->$property == $b->$property)
            {
                return 0;
            }      

            return ($a->$property > $b->$property) ? +1 : -1;        
        }

        usort($objects, 'compare_object');
        return $objects;
  }

Any advice appreciated. Thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about anonymous-function