Page 1 of 3
GLOBAL
Posted: Fri Jul 06, 2007 8:11 pm
by nathanr
Am I the only developer out there who has never used any form of global in any php application? is there any need for them?
what are your opinions and could you give me an example of why and indeed where I should/could be using them.
I know this is rather open ended, but I do have some strong opinions on coding practises and this is one area I'd like to hear opinions on before coming back with my own thoughts

Posted: Fri Jul 06, 2007 9:32 pm
by feyd
You've used globals.
Posted: Fri Jul 06, 2007 10:00 pm
by Christopher
Oh yeah
Posted: Sat Jul 07, 2007 7:29 am
by vigge89
With risk of sounding like a newbie: Globals? $GLOBALS[], global $var;, global functions or what?

Posted: Sat Jul 07, 2007 11:16 am
by superdezign
Globals are not the devil, you know. In inexperienced hands, they can cause problems, but otherwise they speed up both the production process and the execution.
Posted: Sat Jul 07, 2007 11:24 am
by Chris Corbyn
I was going to reply to this thread but I realised how pointless it would be, so I replied anyway.
Re: GLOBAL
Posted: Sat Jul 07, 2007 11:26 am
by superdezign
nathanr wrote:Am I the only developer out there who has never used any form of global in any php application?
You don't use _POST or _GET? You're one amazing kid.
Posted: Sat Jul 07, 2007 12:16 pm
by Christopher
There is a difference between declaring variables in the global scope and accessing variables in the global scope from within functions and classes. It is the latter that is considered a poor practice because the possibility for multiple dependencies which can lead to unexpected side effects and difficult to maintain code.
I think most PHP programs declare some variables in the global scope. Limiting the number of these may be a good practice, but trying to eliminate any variables in the global scope may lead to design workarounds.
Re: GLOBAL
Posted: Sat Jul 07, 2007 1:44 pm
by stereofrog
superdezign wrote:nathanr wrote:Am I the only developer out there who has never used any form of global in any php application?
You don't use _POST or _GET? You're one amazing kid.
Unfortunately, it's not possible to get rid of superglobals completely, but there is no good reason to use them more than once in the whole program.
As to globals in general, if your index.php doesn't look like this
it's time to review your design practices.

Re: GLOBAL
Posted: Sat Jul 07, 2007 1:59 pm
by Ollie Saunders
stereofrog wrote:As to globals in general, if your index.php doesn't look like this
it's time to review your design practices.

Yep that's pretty much what mine looks like. A couple of requires first. I voted that I never use them but a couple of days ago I used about 8 or so in a CLI script. But the whole thing was simple enough for that not to be a problem. I didn't declare any classes either for instance. As with anything generally deemed hacky, don't use them, unless you are putting together something small and quick when they'll usually speed things up.
Re: GLOBAL
Posted: Sat Jul 07, 2007 2:06 pm
by superdezign
stereofrog wrote:As to globals in general, if your index.php doesn't look like this
it's time to review your design practices.

Damn.
Code: Select all
include_once('config.php');
$pPageDisplay->ShowPage();
Re: GLOBAL
Posted: Sat Jul 07, 2007 2:55 pm
by Christopher
stereofrog wrote:
As to globals in general, if your index.php doesn't look like this
it's time to review your design practices.

Perhaps you could explain why doing that as opposed to doing what is in Application's constructor in the global scope is a better practice? And I assume that you also would need to include the class or an autoload function:
Re: GLOBAL
Posted: Sun Jul 08, 2007 8:59 am
by stereofrog
arborint wrote:stereofrog wrote:
As to globals in general, if your index.php doesn't look like this
it's time to review your design practices.

Perhaps you could explain why doing that as opposed to doing what is in Application's constructor in the global scope is a better practice?
Because Application is going to initialize itself in the constructor.
And I assume that you also would need to include the class or an autoload function:
Another option would be auto_prepend the default autoload behaviour to keep things clean.

Posted: Sun Jul 08, 2007 9:26 am
by Oren
I'm sorry, but that's a stupid answer - simply because there is no good answer. Doing that as opposed to doing what is in Application's constructor cannot be justified, it's pretty much like taking everything in the index.php file and throw it into some other file and then include this other file from index.php - this will take the same time as without doing it at best, and will probably be slower at worst.
Posted: Sun Jul 08, 2007 10:13 am
by stereofrog
Oren wrote:I'm sorry, but that's a stupid answer - simply because there is no good answer.
Please don't lower the level of the discussion with arguments like this.
Doing that as opposed to doing what is in Application's constructor cannot be justified, it's pretty much like taking everything in the index.php file and throw it into some other file and then include this other file from index.php - this will take the same time as without doing it at best, and will probably be slower at worst.
I'm not sure what you're trying to say here. Can you provide an example of your approach?