<?php /** * @author v.r And * * @example * 工廠模式 + 單例模式 * 提供一系列不一樣類型單例對象的工廠 * 列子: * @copyright copyright information * */ class SingleFactory { private static $Objects = array(); private static function __construct() {} private static function __clone() {} public static function create($className) { $model_class = $model_name . 'Model'; if(!isset(self::$models[$model_name])) { self::$models[$model_name] = new $model_class(); } return self::$Objects[$model_name]; } } class UserModel { public function FunctionName() { echo '11'; } } class CatModel { public function FunctionName() { echo 'ccc'; } } $userModel = SingleFactory :: create('User'); $catModel = SingleFactory :: create('Cat'); $userModel1 = SingleFactory :: create('User'); $catModel1 = SingleFactory :: create('Cat'); var_dump($userModel); var_dump($catModel); var_dump($userModel1); var_dump($catModel1); #end script