PHP 3 basic Design Patterns in combination using _php tutorial

Source: Internet
Author: User

PHP 3 Basic Design Patterns combined use


1.1 工厂模式 , the factory method or class generates the object instead of the code directly in the new

    class Factory{              static function getDatabase(){            return new Mysql($host, $user, $pass);        }    }    #使用    Factory::getDatabase();

1.2 单例模式 , making an object of a class run only to create a

1. There is a private static object variable, specifically for the object of this class 2. There is a static method to create object 3. There is a private constructor that prevents the external new object from being 4. There is a clone method to prevent clone return False
Reference Article singleton mode
Class Database {//Single object property private static $instance;      Define some global variables that need to hold the property private $props = Array ();      Private Function __construct () {echo ' into construct! the class does not allow external creation of objects ';  }//Returns a singleton public static function getinstance () {//To determine if an instantiated object already exists if (Empty (self:: $instance))              {//Can be override (dynamic parsing) Self:: $instance = new static ();          Cannot be override (static parsing)//self:: $instance = new self ();      } return Self:: $instance;    } public Function __clone () {return ' This class prohibits clone ';      }//Set property public function SetProperty ($key, $value) {$this->props[$key] = $value;      }//Get property Public Function Getpeoperty ($key) {return $this->props[$key];  }}//Use $DBOBJ = Database::getinstance ();  $DBOBJ->setproperty (' Root_path ', '/www ');  $DBOBJ->setproperty (' Tmp_path ', '/tmp '); Next, delete the Singleton object, and if you can get to the property you just added, use theIs the same object unset ($DBOBJ);  $DBOBJ = Database::getinstance ();  echo $dbObj->getpeoperty (' Root_path ');   echo $dbObj->getpeoperty (' Tmp_path ');

1.3 Registration mode, global share and Swap objects

1. The same need to use the unified registration of the object to add aliases, unified call to use, (such as customers buy a machine must go to the factory to buy, not everyone to the factory to buy) 2. The next time you want to use an object, you don't need to use a factory, and you don't need to use a singleton mode. Get it directly on the registrar.
    class Register (){        protected static $objects;        function set($alias, $object){            self::$objects[$alias] = $objects;        }        function get($alias){            return self::$objects[$alias];        }        function _unset($alias){            unset(self::$objects[$alias]);        }    }

2. Summary use

    class Factory{              static function getDatabase(){            //单例模式获取数据对象            $dbObj = Database::getInstance();            //注册到全局树上            Register::set('db1', $dbObj);        }    }    #使用    //第一次主文件里面    Factory::getDatabase();    //以后使用数据库对象直接访问    Register::get('db1');

http://www.bkjia.com/PHPjc/1030445.html www.bkjia.com true http://www.bkjia.com/PHPjc/1030445.html techarticle PHP 3 Basic design pattern combinations Use 1.1 Factory mode, factory method or class to generate objects, instead of code directly new class factory{static function Getdatabase () {return n ...

  • 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.