News Feed
Sections




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

PHPit.net:
Using globals in PHP
June 27, 2006 @ 14:53:34

In this new tutorial from PHPit.net today, Dennis Pallett talks about using globals in PHP, desscribing what they are and how to use them.

Whenever you're developing a new large-scale PHP script, you're bound to use global variables, since some data needs to be used by multiple parts of your script. Good examples of global data are script settings, database connections, user credentials and more. There are many ways of making this data global, but the most commonly used way is to use the global keyword, which we will explore later on in this article.

It's good that he mentions right from the start that most global data is a bad idea, and can really start to clutter up an application. He does help the reader prevent this, though, through the use of a few handy techniques (and design patterns).

He looks first at the "global" keyword and its use, followed by three reasons that it's not all that favorable to use. He gives other options for the "just make it global" thinking, including passing the values in function calls, passing by reference, and using the Singleton and Registry patterns to contain things a bit more. He even includes a wrapper for the registry to handle the only other globals left - the superglobals - with the same registry functionality.

0 comments voice your opinion now!
globals using tutorial singleton registry reference argument globals using tutorial singleton registry reference argument


blog comments powered by Disqus

Similar Posts

Codewalkers.com: Book Review - Pro PHP-GTK

NETTUTS.com: Top 15+ Best Practices for Writing Super Readable Code

DevShed: Fetching Search Results as Serialized Arrays with Yahoo Web Services and PHP 5

Community News: Symfony Framework Makes Some Updates (v.0.6.2 and Content)

DevShed: Main Methods of the DirectoryIterator Class in PHP 5


Community Events





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


threedevsandamaybe testing unittest laravel code developer release refactor list framework opinion install api series podcast interview introduction language community symfony2

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