|
Posted by Mladen Gogala on 05/22/06 07:08
On Sun, 21 May 2006 21:41:27 +0100, Paul Lautman 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.
>>
>> 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.
>
> 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.
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
[Back to original message]
|