PHP design mode factory, Singleton, registered tree mode, PHP design mode _php Tutorial

Source: Internet
Author: User

PHP design mode factory, Singleton, registered tree mode, PHP design mode

Source Code Pro Font easyphp

namespaces : Isolating classes and functions, php5.3 later

Phpnamespace Test5; The //namespace must be the first statement of the program script, except for declarefunction Test () { __file__;}
Phpnamespace Test6; function Test () { __file__;}
    Require ' test5.php ';  Require ' test6.php '; Test5\test (); Test6\test ();  

class Auto-load : After php5.2

Spl_autoload_register (' Autoload1 '); TEST5::test (); TEST6::Test ();  function Autoload1 ($classrequire __dir__. ' /'. $class. " php ';}       

PSR-0 Specification :

    • Namespaces must be consistent with absolute paths
    • The first letter of the class name must be uppercase
    • In addition to the entry file, the other ". PHP" must have only one class.

Develop a framework that complies with PSR-0 specifications

SPL Standard library :

PHP chained Operation :

  Phpnamespace Baobab; class database{    function where ($where) {        return$this ;    }         function Order ($order) {        return$this;    }         function Limit ($limit) {        return$this;    }}? >

$db = new Baobab\database ();
$db->where (' id = 1 ')->order (' ORDER by id ')->limit (1);

Magic Method:

    • __get/__set: Takes over object properties. __set () is called when a value is assigned to an inaccessible property, and __get () is called when the value of the Inaccessible property is read.
    • __call/__callstatic: When a non-accessible method is called in an object, __call () is called, and __callstatic () is called when a non-accessible method is called in a static manner.
    • __tostring: Converting a class into a string
    • __invoke: The __invoke () method is called automatically when an object is called as a function call.


 Phpnamespace Baobab;class Object{    protected $array=Array(); function__set ($key,$value){        $this-Array[$key] =$value; }    function__get ($key){        //echo __method__;        return $this-Array[$key]; }        function__call ($func,$param){        //var_dump ($func, $param);        return' Magic function '; }        Static function__callstatic ($func,$param) {        //var_dump ($func, $param); return' Magic static function '; }        function__tostring () {return __class__; }        function__invoke ($param) {        return Var_dump($param); }    }


$obj new baobab\Object(); $obj->title = ' Hello '; Echo $obj, title; Echo $obj->test1 (' Hello ', 123); Echo $obj:: Test1 (' Hello1 ', 1234); Echo $obj ; Echo $obj (' test1 ');

1. Three basic design Patterns

    • Factory mode: Use factory methods or classes to produce objects instead of directly new in code


  Phpnamespace Baobab; class factory{    staticfunction  createdatabase () {        $dbNew  Database ();         return $db ;    }}


$db = baobab\factory::createdatabase ();
$db 1 = baobab\factory::createdatabase ();
$db->limit ($limit);
    • Singleton mode: Allows an object of a class to create only one


  Phpnamespace Baobab; class database{       protectedstatic$db;         Private function __construct () {            }        staticfunction  getinstance () {          If (self::$db) {            return to self::$db;        } Else {            //Self is a pointer to the class itself, that is, the--is not pointing to any object that has already been instantiated (:: domain operation symbol)            :$db New Self ();             return self::$db;        }    }


$db = Baobab\database::getinstance ();
    • registration mode: Resolves global shares and swap objects, registers objects on the global tree, and can be accessed directly from anywhere


  Phpnamespace Baobab; class register{    protectedstatic$objects;     Static function Set ($alias$object) {        self::$objects[$ Alias$object;    }         Static function _unset ($alias) {        unset(self::$objects[  $alias]);    }         Static function Get ($name) {        return self::$objects[$ Name];}    }
//Register DB to the registration tree $db );      


$db = baobab\register::get (' db1 '); true techarticle PHP design mode factory, Singleton, registered tree mode, PHP design mode Source Code Pro Font easyphp namespace: Isolation classes and Functions, php5.3 later//test5.php? phpnamespace Test5 ...

  • Related Article

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