首页 > 代码库 > php工厂设计模式

php工厂设计模式

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<?phpclass Cars{    function __construct(){        echo "汽车类";    }}Animal.php<?phpclass Animal{    function __construct(){        echo ‘动物世界!‘;    }}

 

php工厂设计模式