Using spAutoLoad class
This document describe hoto use spAutoLoad class
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.
<?php
require_once(dirname(__FILE__) . '/../interfaces/iAutoLoadClassSearch.interface.php');
class autoload implements iAutoLoadClassSearch
{
public function doSearch($className)
{
$type = 'class';
$dir = 'classes';
if(substr($className, 0, 1) ==
'i'){
$type = 'interface';
$dir = 'interfaces';
}else if(substr($className, 0, 1) ==
'e'){
$type = 'exception';
$dir = 'exceptions';
}
return false;
}else if(!include(dirname(__FILE__
) .
"/../$dir/$className.$type.php")){
return false;
}else{
if(substr($className, 0, 1) ==
'i'){
return false;
return false;
}}
return true;
}}
?>
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.
<?php
require_once(dirname(__FILE__) . '/classes/spAutoLoad.class.php');
require_once(dirname(__FILE__) . '/classes/autoload.class.php');
spAutoLoad::getInstance()->addSearchClass(new autoload());
?>
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.
<?php
require_once dirname(__FILE__) . '/../interfaces/iAutoLoadError.interface.php';
class autoloaderror implements iAutoLoadError
{
public function onError($className, $outputSearchCLass)
{
$f =
fopen("autoload.log", "a");
mail("webmaster@locahost", "AutoLoad: error class $className not found", "More detail in logs");
die('Site error, administrator reported !!!');
}}
?>
Save it in includes/classes/autoloaderror.php.
Next we need modify includes/application_top.php
This is final version.
<?php
require_once(dirname(__FILE__) . '/classes/spAutoLoad.class.php');
require_once(dirname(__FILE__) . '/classes/autoload.class.php');
require_once(dirname(__FILE__) . '/classes/autoloaderror.class.php');
spAutoLoad::getInstance()->addSearchClass(new autoload());
spAutoLoad::getInstance()->setErrHandler(new autoloaderror());
?>
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.
<?php
class test
{
private $_test = "It's works";
public function test()
{
return $this->_test;
}}
?>
And save it in includes/classes/test.class.php.
First we create file with normal usage of test class.
<?php
require_once(dirname(__FILE__) . '/includes/classes/test.class.php');
$t = new test();
echo $t->test();
?>
If we save this file (./index.php) it's work.
And with autoload
<?php
require_once(dirname(__FILE__) . '/includes/application_top.php');
$t = new test();
echo $t->test();
?>
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.
<?php
require_once(dirname(__FILE__) . '/includes/application_top.php');
$t = new test();
echo $t->test();
$t2 = new test2();
echo $t->test2();
$t3 = new test3();
echo $t->test3();
?>
Example above have only one require to autoload and use 3 classes.
spAutoLoad loads this classes automaticly on first use.