Home arrow PHP arrow Page 3 - An Object-Oriented Approach to Lazy and Eager Loading in PHP 5

Building a basic loader class - PHP

Welcome to the second installment of a series that shows you how to implement lazy and eager loading in PHP 5. Through a strong hands-on approach, this series teaches you how to use these patterns in some typical scenarios. In this way, you'll grasp their underlying logic and learn quickly how to take advantage of their functionality to speed up your own PHP-based programs.

TABLE OF CONTENTS:
  1. An Object-Oriented Approach to Lazy and Eager Loading in PHP 5
  2. Review: eager loading in PHP 5
  3. Building a basic loader class
  4. Using the Loader class in a sample script
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
September 16, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the section that you just read, I'd like to provide you with an example that shows how to eagerly include the previous "User" class by using a stricter object-oriented approach. To accomplish this, I'll code a simple loader class whose main goal will be statically including a specified file into a given script.

The complete definition of this brand new loader mechanism, along with a couple of custom exception classes, is shown below. Look at them, please:

// create custom FileNotFoundException exception class

class FileNotFoundException extends Exception {}

 

// create custom ClassNotFoundException exception class

class ClassNotFoundException extends Exception {}

 

// define 'Loader' class

class Loader {

 

public static function load($class)

{

if (class_exists($class, FALSE))

{

return;

}

$file = $class . '.php';

if(!file_exists($file))

{

throw new FileNotFoundException('File ' . $file . ' not found.');

}

require $file;

unset($file);

if (!class_exists($class, FALSE))

{

eval('class ' . $class . '{}');

throw new ClassNotFoundException('Class ' . $class . ' not found.');

 }

}

}

As depicted above, the "Loader" class is comprised of a single static method called "load()," which implements the logic required to include the definition of a specified class into a calling script. Also, I added a couple of exception subclasses to handle more specifically any errors that might arise when performing the inclusion process.

So far, so good. At this point, I'm pretty sure that you already grasped the underlying logic of the above "Loader" class, right? Then it's time to build a script that uses this loader module to eagerly include the sample "User" class.

Want to see how this will be done? Click on the link that appears below and read the next section.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: