silverorange Code


Swat.SwatAutoloader
/Swat/SwatAutoloader.php at line 23

Class SwatAutoloader

SwatObject
└─SwatAutoloader

public class SwatAutoloader
extends SwatObject

Automatically requires PHP files for undefined classes

This static class is responsible for resolving filenames from class names of undefined classes. The PHP5 spl_autoload function is used to load files based on rules defined in this static class.

To add a new autoloader rule, use the SwatAutoloader::addRule() method.

Copyright:
2006-2007 silverorange
License:
http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
See Also:
SwatAutoloaderRule

Method Summary
static void

addRule(string expression, string replacement, boolean last)

Adds an autoloader rule to the autoloader.

static void

autoload(string class_name)

Provides an opportunity to define a class before causing a fatal error when an undefined class is used.

static string

getFileFromClass(string class_name)

Gets the filename of a class name.

static void

loadRules(string filename)

Loads a list of autoloader rules from a file.

static void

registerAutoload()

Registers SwatAutoload::autoload() as an autoload function with the spl_autoload function.

Method Detail

/Swat/SwatAutoloader.php at line 43

addRule

public static void addRule(string expression, string replacement, boolean last)

Adds an autoloader rule to the autoloader

Parameters:
expression - the class name expression. Uses PERL regular expression syntax.
replacement - the format string of the filename for the class expression.
last - whether or not the added rule is final or not.
See Also:
SwatAutoloaderRule

/Swat/SwatAutoloader.php at line 148

autoload

public static void autoload(string class_name)

Provides an opportunity to define a class before causing a fatal error when an undefined class is used

If an appropriate file exists for the given class name, it is required.

Parameters:
class_name - the name of the undefined class.

/Swat/SwatAutoloader.php at line 121

getFileFromClass

public static string getFileFromClass(string class_name)

Gets the filename of a class name

This method uses the autoloader's list of rules to find an appropriate filename for a class name. This is used by PHP5's __autoload() method to find an appropriate file for undefined classes.

Parameters:
class_name - the name of the class to get the filename for.
Returns:
the name of the file that likely contains the class definition or null if no such filename could be determined.

/Swat/SwatAutoloader.php at line 77

loadRules

public static void loadRules(string filename)

Loads a list of autoloader rules from a file

This format of the file is as follows:

Each line defines a rule. The line is tokenized into fields based on whitespace characters (tab and space). The first field on the line is the rule expression. The next field on the line is the rule replacement. After the rule replacement field, there is an optional field to specify whether of not the rule is final. If this field is present and its value is 1, the rule is final. If the field is present and its value is 0 or if the field is omitted, the rule is not final. Lines beginning with a hash character (#) are ignored.

An example file containing two rules is:

<?php
/^Swat(.*)/            Swat/Swat$1.php
/^Swat(.*)?Exception$/ Swat/exceptions/Swat$1Exception.php 1
?>

Parameters:
filename - the name of the file from which to load the autoloader rules.
See Also:
SwatAutoloader::addRule()

/Swat/SwatAutoloader.php at line 169

registerAutoload

public static void registerAutoload()

Registers SwatAutoload::autoload() as an autoload function with the spl_autoload function

See documentation on SPL autoloading for details.


silverorange Code