Home arrow PHP arrow Page 3 - Registry Design Pattern: Complete Implementation

Defining a basic autoloader class - PHP

Welcome to the conclusion of a three-part article series that shows you how to implement the Registry design pattern in PHP. In this part, we'll take all the classes that we've created so far and put them to work together.

TABLE OF CONTENTS:
  1. Registry Design Pattern: Complete Implementation
  2. Review: building a basic session-based registry
  3. Defining a basic autoloader class
  4. Using the registry classes in a single script
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
July 21, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Since my plan here is to put all of the registries to work together, I'm going to define a basic autoloader class that will include all of these source classes behind the scenes, on request.

Having said that, here's the source code corresponding to the aforementioned autoloader:

(Autoloader.php)

<?php

class Autoloader

{

private static $_instance;

// get Singleton instance of the autoloader

public static function getInstance()

{

if (!self::$_instance)

{

self::$_instance = new self;

}

return self::$_instance;

}

// private constructor

private function __construct()

{

spl_autoload_register(array('Autoloader', 'autoload'));

}

// prevent cloning instance of the autoloader

private function __clone(){}

// autoload a class on demand

public static function autoload($class)

{

$file = $class . '.php';

if (!file_exists($file))

{

require_once 'ClassNotFoundException.php';

throw new ClassNotFoundException('The file containing the requested class was not found.');

}

include $file;

unset($file);

if (!class_exists($class, false))

{

require_once 'ClassNotFoundException.php';

throw new ClassNotFoundException('The requested class was not found.');

}

}

}

(ClassNotFoundException.php)

<?php

class ClassNotFoundException extends Exception{}

If you're used to taking advantage of the built-in autoloading capabilities offered by PHP, then the definition of the above class should be quite familiar to you. All it does is register its static "autoload()" method in the SPL stack, to include classes on demand. In addition, the class throws a couple of custom exceptions at the proper places, but this feature can be modified or even removed.

All right, having already defined a basic autoloader that include classes on request, the last step that must be taken is to set up a script that puts all the registries to work together. This will be done in the final section of this tutorial, so click on the link below and keep reading.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: