Jump to content

Globals standard practices?


OldWolf

Recommended Posts

This is just a quick two part question that's been bugging me recently:

It seems to me the few 'major' php projects I've had a look at (like phpbb) never use $_GLOBAL, nor do they globalize their classes.  Is this just considered sloppy codework, and if so why (this may be obvious, but I look at things the wrong way some times, lol)?

 

Thanks,

OldWolf

Link to comment
https://forums.phpfreaks.com/topic/93766-globals-standard-practices/
Share on other sites

$_GLOBAL is rarely used in any code, because properly written code does not need to use it.

 

"globalize their classes" could mean anything. Can you give an example.

 

Perhaps if you tell us why you believe not using these is bad, we can provide an answer as to why they are or are not.

 

The purpose of a function is to receive some input, perform a function on it, and return the results. Functions compartmentalize code and data and allow you to re-use the same code over and over. By using something like the $_GLOBAL array or the global keyword, you are tying a specific piece of data in the main program to that function, which eliminates the general purpose nature of the function.

 

For comparison purposes, do you know of any built-in php function that requires you to setup a specifically named variable in the main program before you call the function? The answer is no, because for functions to be re-usable and compartmentalized, you don't write them to directly access a main program variable.

$_GLOBAL is rarely used in any code, because properly written code does not need to use it.

 

"globalize their classes" could mean anything. Can you give an example.

 

Perhaps if you tell us why you believe not using these is bad, we can provide an answer as to why they are or are not.

 

The purpose of a function is to receive some input, perform a function on it, and return the results. Functions compartmentalize code and data and allow you to re-use the same code over and over. By using something like the $_GLOBAL array or the global keyword, you are tying a specific piece of data in the main program to that function, which eliminates the general purpose nature of the function.

 

For comparison purposes, do you know of any built-in php function that requires you to setup a specifically named variable in the main program before you call the function? The answer is no, because for functions to be re-usable and compartmentalized, you don't write them to directly access a main program variable.

 

Excellent, thank you, that answers my question.  :)

 

When I said globalize their classes, what I should have said was objects... don't know where my head was at there.

 

I think it also would have helped for me to mention a good example.

 

In phpbb, they frequently use the variable $phpbb_root_path (I'm pretty sure that's the exact name, doing it off memory).  At any rate, they have to global it into a lot of their functions when they want to use it.  The same goes for the $db object.  That's where my question was rooting from... I was simply wondering why they didn't global the two so that they were accessable without globalizing in each function they wanted them in.  :)

Or better yet, OOP would be used and during the initialization step, when the class constructor is executed, any application configuration file or database would be read and used to set any configuration settings (which should in most cases be defined constants) that are used in the rest of the application.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.