Summary of single and Factory mode learning in PHP
_db = Pg_connect (' dbname = example_db ');} Declares a private _clone method in order to eliminate a vulnerability in the PHP language that can replicate objects and thereby break a single responsibility private _clone () {};/** * declaration getinstance () static method (Singleton mode actual construction) This method detects whether a static instance variable has saved an instance of the class. * If it does not contain an instance of itself, then the class is initialized and saved to the $_instance variable. * The next time you access this code, the $_instance variable will save an instance of the class, and the instance will not be initialized again. Finally, this method returns the reference to the instance */public static function getinstance () {if (!) ( Self::$_instance instanceof self) {self::$_instance = new self ();} return self::$_instance;} Public Function Query ($sql) {return pg_query ($this->_db, $sql);//Use $this->_db to execute a query}}/** * Use of the Singleton class */$db = Database::getinstance (); $db->query (' select * from example_table ');/** * Use a pure static class that cannot be instantiated */class SomeClass {// Prevents a class from being treated as an instance using Private function _construct () {}public static function SomeMethod () {//Perform some action}}//////////////////////////// Factory mode: A class that contains a method that is designed to create other objects. /** * Create a basic factory class */class MyObject {//objects will be returned from the factory}class myfactory {public static function factory () {//Return object A new instance of the return new MyObject ();}} Call Method $instance = Myfactory::factory ();///////////////////////////////////Image Object Factory/** * Use factory class to parse picture file */interface IImage {function getheight (); function getwidth (); function getData ();} Class Iamge_png implements IImage {private $_width, $_height, $_data;public function _construct ($file) {$this->_file = $_file; $this->_parse ();} Private Function _parse () {//completion of format parsing work//and filling $_width, $_height, $_data}public function getwidth () {return $this->$_ width;} Public Function GetHeight () {return $this->$_height;} Public Function GetData () {return $this->$_data;}} Class Iamge_jpeg implements IImage {private $_width, $_height, $_data;public function _construct ($file) {$this->_file = $_file; $this->_parse ();} Private Function _parse () {//completion of format parsing work//and filling $_width, $_height, $_data}public function getwidth () {return $this->$_ width;} Public Function GetHeight () {return $this->$_height;} Public Function GetData () {return $this->$_data;}} Class Imagefactory {public static function factory ($file) {$pathParts = PathInfo ($file); switch (Strtolower ($pathParts [' Extension '])) {CASe ' jpg ': $ret = new Image_jpeg ($file); Break;case ' png ': $ret = new Image_png ($file); Break;case ' gif ': $ret = new Image_gi F ($file); break;default;//problem}if ($ret instanceof IImage) {return $ret;} else{//problem}}}//$image = imagefactory::factory ('/path/to/my.jpg ');//$image is now an instance of the Image_jpeg class echo $image GetWidth ();/** * Portable database x uses the factory class to resolve database portability issues. */interface idatabasebindings {public Function userexists ($email);} Class Pgsql implements Idatabasebindings {protected $_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_row ($result > 0)) true:false;} Else{return false;}}} Class MYSQL implements Idatabasebindings () {protected $_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 E Mail = "'. $emailEscaped. ' "; if ($result = mysql_query ($query, $this->$_connection)) {return (mysql_num_rows ($result) > 0) true:false;} Else{return false;}}} Class Databasefactory {public static function factory () {$type = Loadtypefromconfigfile (), switch ($type) {case ' pgsql ': return new Pgsql (); Break;case ' MySQL ': return new MYSQL (); break;}}} Usage $db = Databasefactory::factory (); $db->userexists ([email protected] ');? >