<?php
class Example
{
public static function factory($type)
{
if (include_once 'Drivers/' . $type . '.php') {
$classname = 'Driver_' . $type;
return new $classname;
} else {
throw new Exception ('Driver not found');
}
}
}
?>
Defining this method in a class allows drivers to be loaded on the
fly. If the Example class was a database
abstraction class, loading a MySQL and
SQLite driver could be done as follows:
<?php
$mysql = Example::factory('MySQL');
$sqlite = Example::factory('SQLite');
?>