php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #16185 global required in functions
Submitted: 2002-03-20 09:51 UTC Modified: 2002-03-21 16:52 UTC
From: adam at adeptsoftware dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.1.2 OS: WinXP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: adam at adeptsoftware dot com
New email:
PHP Version: OS:

 

 [2002-03-20 09:51 UTC] adam at adeptsoftware dot 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-03-20 09:58 UTC] 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 10:06 UTC] 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 10:07 UTC] 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:44 UTC] hholzgra@php.net
#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;
 [2002-03-20 10:44 UTC] hholzgra@php.net
bogusified
 [2002-03-20 18:18 UTC] adam at adeptsoftware dot com
Thanks for the suggestions, I think I will stick all my globals in a structure since this won't be changed.

I agree that globals are not good in an object oriented language, but I still disagree about this - I would venture to guess that PHP is used mostly for small scale scripting, and the needs of the many - smallness and simplicity - outweigh the needs of the few poor saps who are trying to write an OS with it.
 [2002-03-21 10:56 UTC] adam at adeptsoftware dot com
Interestingly, the one-line solution for accessing globals seems to be causing PHP.exe to hang.
I have narrowed it down to the following code which makes php die under WinXP (at least it does here)


<?
	session_start();
	session_register("Ses");
	$Ses->UserAdvanced = true;
	function test()
	{
		foreach($GLOBALS as $key => $value) global $$key;	// get access to all globals
	}
	test();
?>
 [2002-03-21 11:20 UTC] hholzgra@php.net
ok, try this then:

foreach($GLOBALS as $key => $value) if($key != "GLOBALS") global $$key;


 [2002-03-21 16:52 UTC] adam at adeptsoftware dot com
That new method makes the problem less common but it still occurs.  Shouldn't PHP produce an error rather than hang anyway?  I have been working on this for 3 hours and I still have the PHP lockups, but it's very unpredictable and I can't seem to find a concise sample to repro.  It's very strange because it doesn't appear to hang on that line, but rather on very innocent stuff long afterwards.  I'm giving up on it for now, but this may be something you guys want to look into further.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 05:01:29 2024 UTC