note 39473 deleted from language.variables.scope by didou
| From: | didou@php.net | Date: | Mon, 19 Apr 2004 11:14:00 +0000 |
| Subject: | note 39473 deleted from language.variables.scope by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-68322@lists.php.net to get a copy of this message | ||
Note Submitter: rob g
----
Here's one that surprised me: If you have a function with an include in it, and you declare a
static var in the function, the include inherits it. So far so good. BUT if you declare that static
var in the include, it _doesn't_ carry over to the function. In other words, it's a
one-way street. An included file inherits the calling scope, but it can't declare static vars
into that scope. I can't decide if that's a reasonable thing or not?
Example:
<?
function includetest(){
static $a;
include('include.php');
}
includetest();
includetest();
includetest();
?>
and include.php is:
<?
if (!$a) $a = 1;
echo $a;
$a++;
?>
Output: 123
Now let's move that static declaration just inside the included file:
<?
function includetest(){
include('include.php');
}
includetest();
includetest();
includetest();
?>
where include.php is:
<?
static $a;
if (!$a) $a = 1;
echo $a;
$a++;
?>
Output is: 111
(php 4.3.2 on osx)