class DbFactory {
private $errmsg = '未找到类文件';
static function factory($className){
$className = strtoupper(substr($className,0,1)).substr($className, 1);
if(include_once($className.'.php')){
return new $className;
}
else{
throw new Exception($this->errmsg);
}
}
}
DbFactory::factory('cars');
DbFactory::factory('animal');
Cars.php

<?php
class Cars{
function __construct(){
echo "汽车类";
}
} Animal.php <?php
class Animal{
function __construct(){
echo '动物世界!';
}
}
05-02 20:47