can't access global variables inside a usort function?

前端 未结 4 1268
终归单人心
终归单人心 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:49

    The code I put in my question was dropped inside a template on bbPress, which is the forum cousin to Wordpress. A friend told me that "Sometimes PHP will act weird if you don't global a variable before you define it, depending on how nested the code is when it's executed - bbPress does some complex includes by the time the template outputs".

    So I tried that and it works:

    global $hi123;
    $hi123 = ' working ';
    

    I'm answering my own question in case another idiot like me finds this in a Google search. :-)

    I'm going to accept VolkerK's answer, though, because the object workaround is pretty clever.

提交回复
热议问题