Req->Doc #50110 [Opn->Ver]: Static (File Scope) Variables
| From: | cmb@php.net | Date: | Sun, 08 Jul 2018 19:38:42 +0000 |
| Subject: | Req->Doc #50110 [Opn->Ver]: Static (File Scope) Variables | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-15858@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=50110&edit=1
ID: 50110
Updated by: cmb@php.net
Reported by: n8grndr1087 at optonline dot net
Summary: Static (File Scope) Variables
-Status: Open
+Status: Verified
-Type: Feature/Change Request
+Type: Documentation Problem
Package: Scripting Engine problem
Operating System: *
PHP Version: *
Block user comment: N
Private report: N
New Comment:
Static variables in the global scope work just like
inside function scope: the are initialized at most once,
see <https://3v4l.org/MpDqn>.
However, a (static) variable which is declared in file scope,
may not necessarily be declared in the global scope, e.g.
test.php
<?php
function foo() {
include './a.php';
}
var_dump($bar);
?>
a.php
<?php
static $bar = 42;
?>
I agree that this should be documented.
However, changing the current behavior would constitute a BC
break, and as such would need discussion on the internals@ mailing
list, and possibly the RFC process.
Previous Comments:
------------------------------------------------------------------------
[2012-08-11 17:44:30] youdontneedme at o2 dot pl
There is no information about such usage in documentation. (well, maybe that's why it does not
work)
But if such usage is not documented why syntax:
static $age = 18;
is *valid* outside class?
I agree, this should have same consequences as in C lang.
------------------------------------------------------------------------
[2009-11-07 18:19:38] n8grndr1087 at optonline dot net
This feature would also be useful for static (file scope) functions as well.
------------------------------------------------------------------------
[2009-11-07 05:07:19] n8grndr1087 at optonline dot net
Description:
------------
Global variables should be able to be declared 'static'.
Static globals, like in C, would only be accessible from the current file's scope. This would
be a good way to protect internal variables used behind the scenes in a particular file.
If there is any effective way to protect variables that I overlooked, outside of PHP classes, let me
know. I understand that the possibility of this depends on how the include/file processing mechanism
works.
Reproduce code:
---------------
myvar.inc:
<?php
static $myvar = 1;
function get_myvar()
{
global $myvar; // Since its not a true global, this may be different
return $myvar;
}
function set_myvar($v)
{
global $myvar;
$myvar = $v;
}
?>
test.php:
<?php
include('myvar.inc');
echo get_myvar().'<br/>';
set_myvar(0);
$myvar = 10000;
echo get_myvar();
?>
Expected result:
----------------
1
0
Actual result:
--------------
1
10000
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=50110&edit=1