GLOBAL

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

I Use Globals

I do I do all the time!
1
4%
They're useful sometimes
3
13%
I use them here and there but try not
7
30%
never use, why would i?
12
52%
 
Total votes: 23

User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

GLOBAL

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You've used globals.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Oh yeah
(#10850)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

With risk of sounding like a newbie: Globals? $GLOBALS[], global $var;, global functions or what? :o
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I was going to reply to this thread but I realised how pointless it would be, so I replied anyway.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: GLOBAL

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: GLOBAL

Post 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

Code: Select all

new Application();
it's time to review your design practices. ;)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: GLOBAL

Post by Ollie Saunders »

stereofrog wrote:As to globals in general, if your index.php doesn't look like this

Code: Select all

new Application();
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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: GLOBAL

Post by superdezign »

stereofrog wrote:As to globals in general, if your index.php doesn't look like this

Code: Select all

new Application();
it's time to review your design practices. ;)
Damn.

Code: Select all

include_once('config.php');
$pPageDisplay->ShowPage();
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: GLOBAL

Post by Christopher »

stereofrog wrote: As to globals in general, if your index.php doesn't look like this

Code: Select all

new Application();
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:
(#10850)
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: GLOBAL

Post by stereofrog »

arborint wrote:
stereofrog wrote: As to globals in general, if your index.php doesn't look like this

Code: Select all

new Application();
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. ;)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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?
Post Reply