PHP design mode factory, Singleton, registered tree mode, PHP design mode
Source Code Pro Font easyphp
namespaces : Isolating classes and functions, php5.3 later
test5.php
Phpnamespace Test5; The //namespace must be the first statement of the program script, except for declarefunction Test () { __file__;}
test6.php
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; }}? >
index.php
$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.
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 Patterns
- Factory mode: Use factory methods or classes to produce objects instead of directly new in code
factory.php
Phpnamespace Baobab; class factory{ staticfunction createdatabase () { $dbNew Database (); return $db ; }}
index.php
$db = baobab\factory::createdatabase ();
$db 1 = baobab\factory::createdatabase ();
$db->limit ($limit);
- Singleton mode: Allows an object of a class to create only one
database.php
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; } }
index.php
$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
register.php
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 );
index.php
$db = baobab\register::get (' db1 ');
http://www.bkjia.com/PHPjc/1103615.html www.bkjia.com true http://www.bkjia.com/PHPjc/1103615.html 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 ...