Posted by Paul Lautman on 05/22/06 13:11
Rik wrote:
> Chung Leong wrote:
>> Paul Lautman wrote:
>>> Here's something that I can't manage to find explicitly documented.
>>> In the following snippet:
>>>
>>> function outer() {
>>> global $a;
>>> function inner() {
>>> global $a;
>>> echo $a.'1<br>';
>>> }
>>> $a = 's';
>>> inner();
>>> echo $a.'2<br>';
>>>
>>> }
>>> outer();
>>>>
>>>
>>> If either of the globals statements is removed, the variable is not
>>> accessable within inner.
>
> Which is abolutely logical.
>
>> What you're trying to do won't work if you call outer() more than
>> once.
>> PHP has no support for closure or inner functions.
>
>
> You could circumvent this by:
> if(!function_exists('inner'){
> //define function
> }
>
> I'm very curious though why one would need such an imho messy function
> declaration.
>
> Grtz,
I don't actually need it. What happened was that I was having trouble
getting a variable to be global so that I could use a single callback
function for my usort and have it sort on different "fields" in my array. It
worked OK in a test setup, but not in the application. Turns out that I
hadn't realised that in the application, the callback function was declared
inside another function, whereas in the test setup it was at the top level.
[Back to original message]
|