ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> php-dev
php-dev
#50110 [NEW]: Static (File Scope) Variables
by N8grndr1087 At Optonline Dot Net other posts by this author
Nov 6 2009 9:07PM messages near this date
#50111 [Opn]: memory leak when stream_context_create is used | #50110 [Opn]: Static (File Scope) Variables
From:             n8grndr1087 at optonline dot net
Operating system: Unix
PHP version:      5.2.11
PHP Bug Type:     Feature/Change Request
Bug description:  Static (File Scope) Variables

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 bug report at http://bugs.php.net/?id=50110&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=50110&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=50110&r=trysnapshot53
Try a snapshot (PHP 6.0):            http://bugs.php.net/fix.php?id=50110&r=trysnapshot60
Fixed in SVN:                        http://bugs.php.net/fix.php?id=50110&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=50110&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=50110&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=50110&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=50110&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=50110&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=50110&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=50110&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=50110&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=50110&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=50110&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=50110&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=50110&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=50110&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=50110&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=50110&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=50110&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=50110&r=mysqlcfg
Thread:
N8grndr1087 At Optonline Dot Net
N8grndr1087 At Optonline Dot Net

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved