Combination of php3 basic design modes

Source: Internet
Author: User
The combination of php3 Basic Design Patterns uses the 1.1 factory model, factory methods or classes to generate objects, instead of directly creating new
Class Factory {static function getDatabase () {return new Mysql ($ host, $ user, $ pass) ;}# use Factory: getDatabase ();
1.2 Singleton mode, so that the object of a class can only run to create one
  • 1. there is a private static object variable that stores the objects of this class.
  • 2. there is a static method to create an object.
  • 3. there are private constructors to prevent external new objects
  • 4. there is a clone method to prevent clone return false
    Reference Document Singleton mode
  • Class Database {// single object attribute private static $ instance; // defines some global variables and needs to store the attribute private $ props = array (); // private construct () {echo 'into construct! This class does not allow external creation of objects ';} // returns the public static function getInstance () of a single instance {// determines whether an instantiated object if (empty (self :: $ instance) {// Can be override (dynamic resolution) self: $ instance = new static (); // cannot be override (static resolution) // self :: $ instance = new self ();} return self: $ instance;} public function _ clone () {return 'Clone prohibited in this class ';} // Set the attribute public function setProperty ($ key, $ value) {$ this-> props [$ key] = $ value;} // Obtain the attribute public function getPeoperty ($ key) {return $ this-> props [$ key] ;}// use $ dbObj = Database: getInstance (); $ dbObj-> setProperty ('root _ path ', '/www'); $ dbObj-> setProperty ('tmp _ path', '/tmp'); // delete the singleton object, if you can still get the added property, it means that the same object unset ($ dbObj); $ dbObj = Database: getInstance (); echo $ dbObj-> getPeoperty ('root _ path'); echo $ dbObj-> getPeoperty ('tmp _ path ');
    1.3 registration mode, global sharing and OBject EXchange
  • 1. add aliases for the same object that needs to be used multiple times, and call and use them in a unified manner. (for example, if a customer buys a machine, it must be bought by an organization recognized by the factory, instead of buying them at the factory)
  • 2. if you want to use an object at the moment, you do not need to use the factory or the Singleton mode. you just need to get it directly on the register.
  •     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
    Class Factory {static function getDatabase () {// obtain the data object in Singleton mode $ dbObj = Database: getInstance (); // Register it with the Global Tree Register :: set ('db1', $ dbObj) ;}# use // Factory: getDatabase () in the first master file; // access Register directly using the database object :: get ('db1 ');

    Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

    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.