Php design pattern factory, Singleton, registration tree pattern, php design pattern _ PHP Tutorial

Source: Internet
Author: User
Php design mode: Factory, Singleton, registration tree, and php design mode. Php design mode factory, Singleton, registration tree mode, php design mode SourceCodePro font easyphp namespace: isolation class and function, test5.php after php5.3? PhpnamespaceTest5 php design mode factory, Singleton, registration tree mode, php design mode

Source Code Pro font easyphp

Namespace: Isolate classes and functions, after php5.3

// Test5.php
// Test6.php
   require 'test5.php';require 'test6.php';Test5\test();Test6\test();

Class automatically loaded: After php5.2

spl_autoload_register('autoload1');Test5::test();Test6::test();function autoload1($class){    require __DIR__.'/'.$class.'.php';}

PSR-0 specification:

  • The namespace must be consistent with the absolute path
  • The first letter of the class name must be capitalized.
  • Except for the entry file, ". php" must have only one class.

Develop basic frameworks that comply with PSR-0 specifications

Spl standard library:

PHP chained operations:

   

//index.php
$db = new Baobab\Database();
$db->where('id = 1')->order('order by id')->limit(1);

Magic method:

  • _ Get/_ set: Takes over object attributes. When you assign a value to an inaccessible attribute, __set () is called. when you read the value of an inaccessible attribute, __get () is called.
  • _ Call/_ callStatic: When an inaccessible method is called in an object, __call () is called. When an inaccessible method is called in a static method, __callstatic () will be called.
  • _ ToString: converts a class to a string.
  • _ Invoke: when an object is called by calling a function, the __invoke () method is automatically called.

Object. php

   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);    }    }

Index. php

$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 modes

  • Factory mode: use factory methods or class production objects, instead of directly creating new

Factory. php

   

Index. php

$db = Baobab\Factory::createDatabase();
$db1 = Baobab\Factory::createDatabase();
$db->limit($limit);
  • Singleton mode: allows only one class object to be created.

Database. php

    

Index. php

$db = Baobab\Database::getInstance();
  • Registration mode: implements global sharing and exchange of objects. you can register an object on the global tree and directly access it anywhere.

Register. php

     
// Register the database to the registration Tree Register: set ('db1', $ db );

Index. php

$db = Baobab\Register::get('db1');

Workshop factory, Singleton, registration tree mode, php design Mode Source Code Pro font easyphp namespace: isolation class and function, after php5.3 // test5.php? Phpnamespace test5...

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.