PHP single sample mode and factory mode Learning Summary-php Tutorial

Source: Internet
Author: User
PHP single sample mode and factory mode learning summary & lt ;? Php Singleton mode ** centralized control of database connection responsibilities * declares a singleton class * classDatabase {private $ _ db; declare a private instance variable (when constructing an object, the value of this variable will be summarized by the single-instance mode and factory mode in PHP.
 _ Db = pg_connect ('dbname = example_db ');} // declares the private _ clone method. in order to eliminate a vulnerability in php that can copy objects to undermine a single responsibility, private _ clone () {};/*** declare the getInstance () static method (constructed in Singleton mode). This method checks whether the static instance variable has saved an instance of this class. * If it does not contain an instance, the class will be initialized and saved to the $ _ instance variable. * When you access this code next time, the $ _ instance variable will save an instance of the class and the instance will not be initialized again. Finally, this method returns the instance reference */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}/*** to use the singleton class */$ db = Database: getInstance (); $ db-> query ('select * from example_table '); /*** use a pure static class that cannot be instantiated */class SomeClass {// prevent the class from being treated as an instance using private function _ construct () {} public static function SomeMethod () {// perform some operations }}/////////// //////////////////////////////////////// ///// Factory mode: contains a class specifically used to create other objects. /*** Create a basic factory class */class MyObject {// The object will return from the factory} class MyFactory {public static function factory () {// return object return new MyObject () ;}// call method $ instance = MyFactory: factory (); //////////////////////////////////////// /// Image Object Factory/*** use factory class to parse image files */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 () {// Complete formatting and parsing // fill in $ _ 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 () {// Complete formatting and parsing // fill in $ _ 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_J PEG ($ file); break; case 'PNG ': $ ret = new Image_PNG ($ file); break; case 'GIF': $ ret = new Image_GIF ($ file ); break; default; // PROBLEM} if ($ ret instanceof IImage) {return $ ret;} else {// problem }}// $ image = ImageFactory :: factory ('/path/to/my.jpg'); // $ image is now an example of the Image_JPEG class echo $ image-> getWidth (); /*** portable database × use the factory class to solve the database portability problem. */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 email = "'. $ emailEscaped. '"'; if ($ result = m Ysql_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] ');?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.