Using spAutoLoad class

This document describe hoto use spAutoLoad class

by Wojciech Matusiak wmatusiak@gmail.com

Table of Contents

Prepear spAutoLoad for usage
Adding error handling
Using prepered spAutoLoad

Prepear spAutoLoad for usage

First we need define class wich search orther classes, and add object of this class to spAutoLoad. This exemaple show my search class used for my projects.

  1. <?php
  2.   require_once(dirname(__FILE__) .  '/../interfaces/iAutoLoadClassSearch.interface.php');
  3.   class autoload implements iAutoLoadClassSearch
  4.   {
  5.     public function doSearch($className)
  6.     {
  7.       $type = 'class';
  8.       $dir = 'classes';
  9.       if(substr($className, 0, 1) == 'i'){
  10.         $type = 'interface';
  11.         $dir = 'interfaces';
  12.       }else if(substr($className, 0, 1) == 'e'){
  13.         $type = 'exception';
  14.         $dir = 'exceptions';
  15.       }
  16.       if(!file_exists(dirname(__FILE__) . "/../$dir/$className.$type.php")){
  17.         return false;
  18.       }else if(!include(dirname(__FILE__) . "/../$dir/$className.$type.php")){
  19.         return false;
  20.       }else{
  21.         if(substr($className, 0, 1) == 'i'){
  22.           if(!interface_exists($className, false)){
  23.             return false;
  24.           }}else if(!class_exists($className, false)){
  25.           return false;
  26.         }}
  27.       return true;
  28.     }}
  29. ?>
Save this in file include/classes/autoload.class.php. Now we write file to load spAutoLoad and prepear it to use. This file wil by included in all files in project.
  1. <?php
  2.   require_once(dirname(__FILE__) . '/classes/spAutoLoad.class.php');
  3.   require_once(dirname(__FILE__) . '/classes/autoload.class.php');
  4.  
  5.   spAutoLoad::getInstance()->addSearchClass(new autoload());
  6. ?>
Save it in includes/application_top.php

Adding error handling

In this part we define new error handling class and set spAutoLoad to use it. First we define new error handling class.

  1. <?php
  2.   require_once dirname(__FILE__) . '/../interfaces/iAutoLoadError.interface.php';
  3.   class autoloaderror implements iAutoLoadError
  4.   {
  5.     public function onError($className, $outputSearchCLass)
  6.     {
  7.       $f = fopen("autoload.log", "a");
  8.       fputs($f, date("Y-m-d h:i:s") . "\t$className\t" . str_replace("\t", "    ", nl2br($outputSearchCLass)));
  9.       fclose($f);
  10.       mail("webmaster@locahost", "AutoLoad: error class $className not found", "More detail in logs");
  11.       die('Site error, administrator reported !!!');
  12.     }}
  13. ?>
Save it in includes/classes/autoloaderror.php. Next we need modify includes/application_top.php This is final version.
  1. <?php
  2.   require_once(dirname(__FILE__) . '/classes/spAutoLoad.class.php');
  3.   require_once(dirname(__FILE__) . '/classes/autoload.class.php');
  4.   require_once(dirname(__FILE__) . '/classes/autoloaderror.class.php');
  5.  
  6.   spAutoLoad::getInstance()->addSearchClass(new autoload());
  7.   spAutoLoad::getInstance()->setErrHandler(new autoloaderror());
  8. ?>

Using prepered spAutoLoad

Ok we have prepered spAutoLoad to use, now we create simple test and show how it works. First we create test class.

  1. <?php
  2.   class test
  3.   {
  4.     private $_test = "It's works";
  5.     public function test()
  6.     {
  7.       return $this->_test;
  8.     }}
  9. ?>
And save it in includes/classes/test.class.php. First we create file with normal usage of test class.
  1. <?php
  2.   require_once(dirname(__FILE__) . '/includes/classes/test.class.php');
  3.   $t = new test();
  4.   echo $t->test();
  5. ?>
If we save this file (./index.php) it's work. And with autoload
  1. <?php
  2.   require_once(dirname(__FILE__) . '/includes/application_top.php');
  3.   $t = new test();
  4.   echo $t->test();
  5. ?>
In line 2 autoload loads test class from includes/classes/test.class.php. We have only one class in this example but if we have more then one we can use only one require and all classes used in file will by automaticli loaded.
  1. <?php
  2.   require_once(dirname(__FILE__) . '/includes/application_top.php');
  3.   $t = new test();
  4.   echo $t->test();
  5.   $t2 = new test2();
  6.   echo $t->test2();
  7.   $t3 = new test3();
  8.   echo $t->test3();
  9. ?>
Example above have only one require to autoload and use 3 classes. spAutoLoad loads this classes automaticly on first use.

Documentation generated on Wed, 04 Apr 2007 21:29:38 +0200 by phpDocumentor 1.3.1