can't access global variables inside a usort function?

前端 未结 4 1235
终归单人心
终归单人心 2021-01-15 16:32

I\'m trying to do a usort in PHP, but I can\'t access global variables inside a usort function.

I\'ve simplified my code down to bare bones to show what I mean:

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-15 16:43

    Can't reproduce the "error" and neither can codepad: http://codepad.org/5kwctnDP

    You could also use object properties instead of global variables

    test;
            return strcmp($a, $b);
        }
    }
    
    
    $topics = array(1,2,3);
    $foo = new Foo;
    usort($topics, array($foo, 'bar'));
    

提交回复
热议问题