Every once in a while I discover a really cool, really underutilized component in PHP and I just have to write about it. I recently discovered (through my use of Zend Framework) the implementation of the ArrayObject class.
ArrayObject is an object that is designed to behave exactly like an array. If that seems confusing, don’t worry, it’s not. ArrayObject is an object. It follows all the rules of how objects work. But it’s designed to implicitly behave like an array for all intents and purposes, including being used in a foreach loop, and accessing it’s properties just like you would access the values in an array. Consider the following code sample:
How many of us have seen this example in code we’ve worked on?
< ?php require '/path/to/lib/DatabaseI.php'; require '/path/to/lib/Database.php'; require '/path/to/lib/Authenticate.php'; require '/path/to/lib/User.php'; require '/path/to/lib/BlogEntry.php'; require '/path/to/lib/Comments.php'; require '/path/to/lib/TemplateLoader.php'; ?>