In reality, building a script that lazy-loads the previous "User" class is a straightforward process that can be accomplished in a few simple steps, thanks to the brand new functionality that I gave the pertinent loader module. To demonstrate the veracity of my claim, below I coded that script for you, so you can quickly grasp how it works. Here it is: // create instance of 'User' class only when the script requires it try { // create instance of 'User' class $user = new User(); echo $user; } catch (FileNotFoundException $e){ echo $e->getMessage(); exit(); }
catch (ClassNotFoundException $e){ echo $e->getMessage(); exit(); } Do you realize how simple it is to switch from a script that eagerly loads a sample class to another one that lazy-loads it? And the best part is that this change was accomplished by means of a small modification introduced in the loader module. What else can one ask for? Feel free to tweak all of the code samples that you saw in this tutorial. Doing so will give you a better understanding of how to apply the lazy loading pattern in PHP 5. The experience will be pretty educational, believe me. Final thoughtsIn this third episode of the series, I explained how to improve the performance of a script in PHP 5 by using the lazy loader pattern. As you saw for yourself, the proper usage of the pattern allowed us to include a class only when it was requested, thus avoiding unnecessary overheads during the script's execution. Logically, it's possible to utilize the pattern when instantiating classes, a procedure not surprisingly known as lazy instantiation. Nonetheless, this topic is outside of the scope of this series of articles, so it's time to focus on the concepts that will be deployed in the upcoming part. In that tutorial, I'm going to discuss how to use eager loading when working with the properties of a class. So, here's my little piece of advice: don't miss the next article!
blog comments powered by Disqus |
|
|
|
|
|
|
|