To be frank, there are many situations where it’s possible to use the ArrayObject class as a base implementation and get pretty satisfactory results. However, one case where this is especially useful occurs when you need to create a view class that gets assigned a set of properties dynamically, which are later populated within an associated template file. If you're wondering how such a view class looks, just check out the example below: <?php namespace ArrayObject; class View extends \ArrayObject From the previous code snippet, it’s easy to see the inner workings of the View class. Aside from extending ArrayObject, the class implements a few additional methods. These allow it to render the associated template file, set a new one and even reset it to a default value. With this sample view class already up and running, the next step is to put it into action. Say a default template looks like this: (default_template.php) <!doctype html> In this case, creating a populated view object and rendering the template could be achieved in the following way: <?php namespace ArrayObject; require_once 'View.php'; // create an instance of the View class and assign some properties to it // render the view (uses the default template file) That was really easy to code and read, wasn’t it? Furthermore, since the flexibility of ArrayObject is actually the backbone of the View class, the earlier script could be rewritten as follows: <?php namespace ArrayObject; require_once 'View.php'; // create an instance of the View class // assign some properties to the view object // render the view (uses the default template file) There you have it. Regardless of the script that best suits your personal taste (it might even be both), the template file will be rendered on screen, and the embedded variables will be properly populated as expected. Of course, it’s possible to further extend the view class, so that it can perform a few more complex tasks. In any case, you can see pretty clearly how to reuse the functionality of ArrayObject in a real world situation. So, go ahead and give the class a try. Closing Remarks In this two-part tutorial, I provided you with a quick introduction to the main features offered by the ArrayObject SPL class, and also showed you how to tweak its behavior by using its STD_PROP_LIST and ARRAY_AS_PROPS constants. Naturally, this lesson would have been somewhat useless without setting up an example that demonstrated how to employ the class in a more realistic situation. To address this issue, I went through the development of a simple view class, which not only extended the core functionality of ArrayObject, but could render a template file and populate the set of properties embedded within it. While this example was pretty basic, it showed how easy it is to exploit the abilities offered by ArrayObject to spawn a refined implementation of it. So, now that you have a clear idea of what this rather overlooked SPL class can do for you, feel free to utilize it when developing your own PHP applications. If you can happily live with its biggest limitation (that it can’t be used with most of the native PHP array functions) it might be worth looking into. See you in the next PHP tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|