#28352 [Com]: Referencing Global Variables
| From: | phpbr at ecartz dot com | Date: | Thu, 22 Jul 2004 04:42:23 +0000 |
| Subject: | #28352 [Com]: Referencing Global Variables | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-63309@lists.php.net to get a copy of this message | ||
ID: 28352
Comment by: phpbr at ecartz dot com
Reported By: hpdl at oscommerce dot com
Status: Bogus
Bug Type: Session related
Operating System: Linux
PHP Version: 4.3.4
New Comment:
There are no relevant "Session Related" bug reports with text
"references", but I did find http://bugs.php.net/bug.php?id=17959
(under "Documentation problem") which includes the cited explanation.
For some reason, the documentation on the global keyword (
http://www.php.net/manual/en/language.variables.scope.php
) does not
mention that the global keyword is merely a hack to set $var &=
$GLOBAL['var'] rather than a persistent mark of a variable as global.
Reading the user comments or going to
http://www.php.net/manual/en/language.references.spot.php
is necessary
to find this out.
Previous Comments:
------------------------------------------------------------------------
[2004-07-06 01:48:26] sniper@php.net
This has been explained in several bug reports. Please read the
'Session related' ones with text 'references' in them..
------------------------------------------------------------------------
[2004-05-10 19:02:18] hpdl at oscommerce dot com
Description:
------------
A global variable which references a session variable
inside a class loses its global scope.
Reproduce code:
---------------
<?php
session_start();
class test {
function test() {
global $b;
if (!isset($_SESSION['a'])) {
$_SESSION['a'] = 0;
}
$b =& $_SESSION['a'];
}
}
$test = new test;
$b++;
echo $b;
echo '<br><br>';
echo '<a href="' . basename($_SERVER['PHP_SELF']) .
((defined('SID') &&
strlen(SID) > 0) ? '?' . SID : '') . '">reload</a>';
?>
Expected result:
----------------
The counter shown should be incremented on each page
reload.
Actual result:
--------------
The counter does not get incremented and stays at 1.
Changing the following line:
$b =& $_SESSION['a'];
to:
$GLOBALS['b'] =& $_SESSION['a'];
produces the expected result.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=28352&edit=1