Bug #16185 Updated: global required in functions
| From: | hholzgra@php.net | Date: | Wed, 20 Mar 2002 15:44:05 +0000 |
| Subject: | Bug #16185 Updated: global required in functions | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-2967@lists.php.net to get a copy of this message | ||
ID: 16185
Updated by: hholzgra@php.net
Reported By: adam@adeptsoftware.com
Status: Open
Bug Type: Feature/Change Request
Operating System: WinXP
PHP Version: 4.1.2
New Comment:
#1 don't use globals. period. we are *not* going to change the language
to support bad design techniques
#2 you can get all global symbols by doing
foreach($GLOBALS as $key => $value) global $$key;
Previous Comments:
------------------------------------------------------------------------
[2002-03-20 10:07:18] derick@php.net
Changing this would break waaay to much, and it's against the nature of
PHP.
i think it's a bad idea to change this.
Derick
------------------------------------------------------------------------
[2002-03-20 10:06:40] mboeren@php.net
I agree with Rasmus (and I really like his story :-), but to help you
shoot yourself in the foot:
Declare and use your globals like this:
$g->id = 1000;
$g->username = "Marc";
// etc. until you have 20 of them
function fn() {
global $g;
$g->username = "Rasmus";
}
with the added benefit of auto-global for any new global var you add in
a later stage of the project.
This looks very similar to using $GLOBALS["username"] inside your
function, which is what I use if I desperately _need_ a global.
Cheerio, Marc.
------------------------------------------------------------------------
[2002-03-20 09:58:22] rasmus@php.net
This simply will not change. It is a characteristic of the language.
Perhaps you shouldn't be using 20 global variables?
See http://marc.theaimsgroup.com/?l=php-general&m=97984136422910&w=2
------------------------------------------------------------------------
[2002-03-20 09:51:10] adam@adeptsoftware.com
"In PHP global variables must be declared global inside a function if
they are going to be used in that function."
I really think PHP should not be different from C here - or at least
there should be a setting for this. Even something like "global ALL;"
would be better, but not ideal.
The most common problem I run into by FAR is variables not being
available in the local scope. Making me register 20 of them as global
by name in every single function is counterproductive and goes against
the idea of keeping code as simple as possible. Your justification for
this is: "This can cause some problems in that people may
inadvertently change a global variable." That is a very rare problem
compared to the new problem this has introduced. JavaScript handles
this better, requiring declaration with "var" to force a variable to be
local only. I would much prefer this, and PHP doing it the way every
other language does would also be preferable.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=16185&edit=1