PDA

View Full Version : Using $GLOBALS is bad programming Practice!? Singleton Registry Better?, an Why?


vi390
12-10-2007, 09:25 AM
in terms of OOP we learn, that using Global values like $GLOBALS in PHP is a bad practice, and if ever Global Values have to be used it should be done with an Singleton Registry Pattern.
I have implemented both methods now, and have found out the following.


*The Registry Singleton is nice, since I can double check values before they get in there with the set method
*I have one capsulation container for Global Data
* I can Protect them for outside view

But on the other hand
* The $GLOBALS Array is just there to use already, its not a single lline of extra code necessary to make it work
* its just dropping some values, and get access there everywhere
* I can check the values with a check Class, and throw errors, if necessary I can call this whenever i want
* I still can use get, and set methods, to use this memory area, but I do not have to if I do not like it, and access it directly
* I can have direct access to the variables and arrays, without having to make some sort of special methods to access it directly
* Values do not have to be copied in the memory mapping to access them and to change them. Its just one place which holds them.
* for protected Data, you still can use a Singleton Registry if you need it, and it makes sence
* If I save something in $GLOBALS with $GLOBALS['myvalues']['part1_array'][] = "Hello World"; I just make print_r($GLOBALS['myvalues']); and I have everything important for Bugfixing. I will have to write an extra Method, If I want to do that with a Singleton Registry.
* Accessing A Global Array is faster, then getting it from Classes, It might be valuable on special Sites with loads of users.


So Iam just asking myself, Why would I have to use the Singleton Registry Method again. I understand that in an environment, where hundreds of Coders are working on a project, and where it has to be sort of an hierarchy of Coding, and protecting variables.
But if the project is some sort of MAIN code, forexample a Main Framework, where its Core is a Major DataBackBone Memspace, where all the values get gathered together. (Iam not talking about doing everything with a Superglobal Array,and forget about parameter passing, this would be Very Bad Practice, Iam just taliking about the absolute need of global available Data, and we all know, that there are situations , where you need that) Iam not talking about the reuse of this classes in every situation. Iam talking about classes which belong together and are not about to be ripped apart. Its a Key functionality of a System, and the $Globals is used with interfaces, not just accessed wildly, and uncontrolled.

I do NOT want to start Flamewars. I Just want to know and discuss Opinions for, or against the use of either $GLOBALS in special cases. And the Singleton Registry Pattern.

mlse
12-10-2007, 01:15 PM
Hi there,

I personally am not the biggest fan of OOP, I am a fan of the KISS principle (Keep It Simple & Straightforward) and I'm also a fan of the YAGNI principle (You Ain't Gonna Need It) (Thanks to GJay (http://www.codingforums.com/showthread.php?t=127381)) for that one!.

That being said, I do tend to ecapsulate stuff in classes simply because it makes the code nice and modular and easy to chop and change as required.

I don't use a single to encapsulate $GLOBALS because I think it's overkill (like a lot of OOP, imho! C++ being a case in point ...) although I can see the benefit IF the Zend developers ever decided that in a future release of PHP they were going to rename $GLOBALS, however I don't think they're likely to do that any time soon!

aedrin
12-10-2007, 05:09 PM
Consider this.

When you put a technology directly in code somewhere, that code is married to it. You will have to go through a long process to divorce it.

In this case, the technology is $_GLOBAL. And your entire website would be using it.

Now, if you want your website to use something else other than GLOBALS, (store them in a text file, database, etc.) you will have to go back and update all of your code. And test everything.

Now think about this situation if you had used a registry class. You update 5-10 lines of code and your life goes on.

I personally am not the biggest fan of OOP, I am a fan of the KISS principle (Keep It Simple & Straightforward) and I'm also a fan of the YAGNI principle (You Ain't Gonna Need It) (Thanks to GJay) for that one!.

That sounds logical, especially in PHP. But you set yourself up for failure. Every language benefits from OOP. Some more than others, but in this case there is enough benefit to use it.

At first it does seem like it makes it more confusing, because now you can no longer look at a single file and see all the code.

But then after you continue developing and maintaining, you notice that changes become easier and cleaner. And you spend much less time testing, because your interface stays the same, so your code will continue to run.

EDIT: Read a programming patterns (http://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional/dp/0201633612/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1197306643&sr=1-1) book. It's enlightening.

felgall
12-10-2007, 07:02 PM
The point of OOP is to keep it simple by keeping things separated.

aedrin
12-10-2007, 07:06 PM
I guess you said the same as I said in a single sentence. :P (I guess KISS should also be applied to conversations)

Fumigator
12-10-2007, 09:06 PM
Nah, I like your long explanations... they help cement the concepts. :thumbsup: Where's that Thank You button...

Inigoesdr
12-10-2007, 09:41 PM
Agreed. :)