基本的工厂类 复制代码 代码如下: class MyObject{ //对象将从工厂返回 } class MyFactory{ public static function factory(){ return new MyObject(): } } $instance=MyFactory::factory();
使用工厂类解析图像文件 复制代码 代码如下: <?php interface IImage{ function getHeight(); function getWidth(); function getData(); } class Image_PNG implements IImage{ private
PHP 工厂模式使用方法_网学
data; } public function getWidth(){ return $this->_width; } public function getHeight(){ return $this->_height; } public function getData(){ return $this->_data; } } class Image_JPEG implements IImage{ private
PHP 工厂模式使用方法_网学
connection; public function __construct(){ $this->_connection=pg_connect(''dbname=example_db''); } public function userExists($email){ $emailEscaped=pg_escape_string($email); $query="select 1 from users where email=''".$emailEscaped."''"; if($result=pg_query($query,$this->_connection)){ return (pg_num_rows($result)>0)?true:false; }else{ return false; } } } class MYSQL implements IDatabaseBindings{ protected
PHP 工厂模式使用方法_网学
connection; public function __construct(){ $this->_connection=mysql_connect(''localhost''); mysql_select_db(''example_db'',$this->_connection); } public function userExists($email){ $emailEscaped=mysql_real_escape_string($email); $query="select 1 from users where email=''".$emailEscaped."''"; if($result=mysql_query($query,$this->_connection)){ return (mysql_num_rows($result)>0)?true:false; }else{ return false; } } } class DatabaseFactory{ public static function fac