wunder-ful.com

PHP Global Variables Effectively Depricated?

Initially I was trying to design things with PHP4 compatibility, and one of the key features PHP4 lacked was static and private variables. To work around the inability to have static variables, I used several singleton classes that had member variables. My cache object, my database abstraction layer, my plugin hook manager were all singleton classes stored in global variables and then imported (via the global keyword) and used in functions where needed.

What I realized however, was that this is generally more code than is really necessary. Importing variables from the global scope is one more line of code. In general most things aren't really truly 'global' in scope, most would be better of stored as a static variable on the class that uses or defines the variable. It's easier to use and conforms to the "separation of concerns" design pattern. I'm probably going to remove 80% of my 20 or so global variables that I had created in CMS.