Oatley
11-06-2012, 10:04 PM
Hello, I have a class that uses the reflection class that Fou-Lu kindly helped me with to load storage drivers like so
//Include all the files that might be needed
include_once 'mysql.class.php';
include_once 'pdo.class.php';
include_once 'xml.class.php';
//etc
class Connection {
public static function createConnection($type) {
$driver = $type . 'Driver';
if (!class_exists($driver)) {
throw new Exception('No drivers for storage type: ' . $type);
}
$ref = new ReflectionClass($driver);
return $ref->getMethod('Connection')->invoke(NULL);
}
}
But rather than including ALL the drivers that I might need before I access my class I wanted to make use the spl_autoload_register feature to load the drivers as and when they are needed.
Is this possible with the above, as I don't get how to do it? As it would be trying to use the spl_autoload_register to load the ReflectionClass each time, as it loads the class name rather than the $type passed in I would have thought?
Is there a way around this? Or any type of other solution?
Thank You
//Include all the files that might be needed
include_once 'mysql.class.php';
include_once 'pdo.class.php';
include_once 'xml.class.php';
//etc
class Connection {
public static function createConnection($type) {
$driver = $type . 'Driver';
if (!class_exists($driver)) {
throw new Exception('No drivers for storage type: ' . $type);
}
$ref = new ReflectionClass($driver);
return $ref->getMethod('Connection')->invoke(NULL);
}
}
But rather than including ALL the drivers that I might need before I access my class I wanted to make use the spl_autoload_register feature to load the drivers as and when they are needed.
Is this possible with the above, as I don't get how to do it? As it would be trying to use the spl_autoload_register to load the ReflectionClass each time, as it loads the class name rather than the $type passed in I would have thought?
Is there a way around this? Or any type of other solution?
Thank You