Connecting Tech Pros Worldwide Forums | Help | Site Map

Variable scope - function in function

Paul Lautman
Guest
 
Posts: n/a
#1: May 21 '06
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.



Chung Leong
Guest
 
Posts: n/a
#2: May 21 '06

re: Variable scope - function in function



Paul Lautman wrote:[color=blue]
> 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.[/color]

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.

Paul Lautman
Guest
 
Posts: n/a
#3: May 21 '06

re: Variable scope - function in function


Chung Leong wrote:[color=blue]
> Paul Lautman wrote:[color=green]
>> 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();[color=darkred]
>>>[/color]
>>
>> If either of the globals statements is removed, the variable is not
>> accessable within inner.[/color]
>
> 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.[/color]

Interesting, that case indeed fails. Yet I came across this problem when
defining a sort function for usort. In that case, one call call the sort
function more than once.


Rik
Guest
 
Posts: n/a
#4: May 21 '06

re: Variable scope - function in function


Chung Leong wrote:[color=blue]
> Paul Lautman wrote:[color=green]
>> 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();[color=darkred]
>>>[/color]
>>
>> If either of the globals statements is removed, the variable is not
>> accessable within inner.[/color][/color]

Which is abolutely logical.
[color=blue]
> 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.[/color]


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,
--
Rik Wasmus


Mladen Gogala
Guest
 
Posts: n/a
#5: May 22 '06

re: Variable scope - function in function


On Sun, 21 May 2006 21:41:27 +0100, Paul Lautman wrote:
[color=blue]
> Chung Leong wrote:[color=green]
>> Paul Lautman wrote:[color=darkred]
>>> 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.[/color]
>>
>> 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.[/color]
>
> Interesting, that case indeed fails. Yet I came across this problem when
> defining a sort function for usort. In that case, one call call the sort
> function more than once.[/color]


Well, it fails complaining about the duplicate declaration, not about the
invocation. This version will work:

#!/usr/local/bin/php
<?php
$a="A";
function outer() {
global $a;
if ($a=="A") {
function inner() {
global $a;
echo $a."1\n";
}
}
$a = "s";
inner();
echo $a."2\n";

}
outer();
outer();
?>
$

This way, the inner function is not re-declared during the second
invocation. This can also be simulated by using eval.


--
http://www.mgogala.com

Paul Lautman
Guest
 
Posts: n/a
#6: May 22 '06

re: Variable scope - function in function


Rik wrote:[color=blue]
> Chung Leong wrote:[color=green]
>> Paul Lautman wrote:[color=darkred]
>>> 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.[/color][/color]
>
> Which is abolutely logical.
>[color=green]
>> 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.[/color]
>
>
> 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,[/color]

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.


Rik
Guest
 
Posts: n/a
#7: May 22 '06

re: Variable scope - function in function


Paul Lautman wrote:[color=blue][color=green][color=darkred]
>>> 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.[/color]
>> 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,[/color]
>
> 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.[/color]


Check, while nesting more and more it becomes hard to find errors like that.
I always try to keep "home-made" funstions in a seperate include for that
reason.

Grtz,
--
Rik Wasmus


Closed Thread