Bug #51686 [Com]: Problem with global scoping
| From: | support at shockandawemedia dot com | Date: | Fri, 15 Nov 2013 17:03:20 +0000 |
| Subject: | Bug #51686 [Com]: Problem with global scoping | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-182778@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=51686&edit=1
ID: 51686
Comment by: support at shockandawemedia dot com
Reported by: quaspam at yahoo dot com
Summary: Problem with global scoping
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Windows
PHP Version: 5.3.2
Block user comment: N
Private report: N
New Comment:
I also wanted to add this, which I just discovered on another related issue which explains why PHP
handles this as it does (for anyone else looking at this issue):
http://www.php.net/manual/en/language.references.spot.php
Previous Comments:
------------------------------------------------------------------------
[2013-11-15 16:52:19] support at shockandawemedia dot com
With all due respect to degeberg@php.net, this is a fundamental design problem in PHP. In any
*real* production grade language, you cannot do away with variables out of the symbol table. Doing
so presents valid problems this user previously mentioned.
I recognize the difference-- PHP allows this. But because of that, developers run into problems not
presented in symbol-table based languages-- for example (in PHP you cannot zero an array unless you
write a function to manually pop all the values out of it and reset its pointer). A better solution
would be to be able to simply unset() and reallocate() the array-- but you can't. Because PHP
doesn't support global scoping as robustly as it should.
<?php
$myVar = array();
/*****
*
* FOO
*
*****/
function foo()
{
global $myVar;
if(!isset($myVar))
{
echo("Set 'myVar' before calling foo()\n");
exit(0);
};
unset($myVar);
$myVar = array();
$myVar[] = array('frost' => 'bite','water' =>
'spiggot','jack' => 'daw');
}
$myVar[] = array('egg' => 'plant','biscuit' =>
'gravy');
foo();
var_dump($myVar);
?>
The above snippet generates the following:
array(1) {
[0]=>
array(2) {
["egg"]=>
string(5) "plant"
["biscuit"]=>
string(5) "gravy"
}
}
Not useful. Instead of marking this user's complaint as 'bogus', trying considering
it as a change/feature request that would improve your language dramatically.
------------------------------------------------------------------------
[2010-04-28 20:20:44] degeberg@php.net
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
------------------------------------------------------------------------
[2010-04-28 20:17:47] quaspam at yahoo dot com
Description:
------------
I had trouble running the following using php 5.3.2:
<?php
$username = 'dummy value';
function fetchUsername(){
global $username;
//Imitate retrieval
$username ='Sample Value';
}
fetchUsername();
echo $username;
?>
The above printed the dummy value (dummy value). Am i missing something? Pliz help
Test script:
---------------
<?php
$username = 'dummy value';
function fetchUsername(){
global $username;
//Imitate retrieval
$username ='Sample Value';
}
fetchUsername();
echo $username; //Prints 'dummy value'
?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=51686&edit=1