News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Brandon Savage's Blog:
Keeping Superglobals Out Of Classes
December 08, 2008 @ 07:57:24

In a new post to his blog, Brandon Savage makes a suggestion that could help in maintenance and debugging down the road - keep those superglobals out of your classes.

Let's ignore the security implications of the above code for just a moment, and focus on just the use of the superglobal. By using the $_POST superglobal array, we're effectively doing two things [in the example code]: relying on the field names and limiting code reuse.

He shows how to refactor the example into something a bit more reusable by changing the method call to pass in the given username and password instead of looking to the global for it. He does note, however, that there are some more correct uses for those superglobals:

There are some legitimate uses of superglobals in classes. One example is the use of the $_SESSION superglobal, which is often used for things like a user object. But I urge you to do so sparingly, when appropriate, rather than relying heavily on superglobals which are subject to change and may not give you the data you expect.
0 comments voice your opinion now!
class object oriented superglobal refactor reuse


blog comments powered by Disqus

Similar Posts

Padraic Brady's Blog: More OpenID (in PEAR and Refactoring)

PHPro.org: Class Hierachies And Overriding

PHP in Action: Type hints are more useful for scalars than objects

Pixelstech.com: Should we use Abstract class or Interface?

Derick Rethans' Blog: PHP Vikinger 2008 Wrap-up


Community Events





Don't see your event here?
Let us know!


podcast voicesoftheelephpant api conference language framework release threedevsandamaybe deployment laravel symfony zendserver bugfix tips interview series code list community introduction

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework