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
<? Phpnamespace Test5; // The namespace must be the first statement of the program script, except for declarefunction test () {echo _ FILE __;}
//test6.php
<?phpnamespace Test6;function test(){ echo __FILE__;}
<?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:
<?phpnamespace Baobab;class Database{ function where($where){ return $this; } function order($order) { return $this; } function limit($limit){ return $this; }}?>
//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
<?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); } }
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
<?phpnamespace Baobab;class Factory{ static function createDatabase(){ $db = new Database(); return $db; }}
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
<? Phpnamespace Baobab; class Database {protected static $ db; private function _ construct () {} static function getInstance () {if (self ::$ db) {return self :: $ db;} else {// self indicates the class itself, that is, self does not point to any instantiated object (: domain operator number) self :: $ db = new self (); return self: $ db ;}}
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
<?phpnamespace Baobab;class Register{ protected static $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 the database to the registration tree Register: set ('db1', $ db );
Index. php
$db = Baobab\Register::get('db1');