note 39473 added to language.variables.scope
| From: | robg at rn2 dot php dot net | Date: | Thu, 29 Jan 2004 18:49:11 +0000 |
| Subject: | note 39473 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64392@lists.php.net to get a copy of this message | ||
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)
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39473
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39473&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39473&report=yes
Search -- http://master.php.net/manage/user-notes.php