①php Advanced Object-oriented features
②11 Type PHP design mode
The most popular technologies such as ③psr-0,composer,phar
The goal is to master all types of PHP design patterns, as well as the ability to design a purely object-oriented framework and system
"Namespaces"
Early PHP has no concept of namespaces, and all code can be thought of as running within the same namespace, so it is not possible to have a class or function with the same name:
PHP 5.3 Adds the concept of namespaces.
Example: Add 3 files under a project:
test1.php:
<? PHP function Test () { echo__file__;}
test2.php:
<? PHP function Test () { echo__file__;}
test.php:
<? phprequire ' test1.php '; require ' test2.php ';
Run test.php, error: Fatal error:cannot redeclare Test () (previously declared in D:\practise\php\design\test1.php:4) in D:\pra ctise\php\design\test2.php on line (function renaming causes fatal error)
When test1.php is changed to:
<? phpnamespace Test1; function Test () { echo__file__;}
test2.php instead:
<? phpnamespace Test2; function Test () { echo__file__;}
Run the test.php without error.
"Methods in the reference namespace"
Namespace name \ Method name
Such as:
test.php
<? PHP require ' test1.php '; require ' test2.php '; Test1\test ();
Page output: D:\practise\php\design\Test1.php
"Auto-loading of classes"
When the project is getting bigger, more and more files, if a file to rely on dozens of PHP classes, it is necessary to write the code before the file dozens of lines of require statement, development and management is very inconvenient. The automatic loading function for classes is provided in versions after PHP 5.2. Get up early PHP uses the __autoload () method.
For example, there are 3 files in a project
test1.php:
<? PHP class test1{ staticfunction Test () { echo__method__ ; }}
test2.php:
<? PHP class test2{ staticfunction Test () { echo__method__ ; }}
test.php:
<? phpTest1::test (); Test2::Test (); function __autoload ($class) { require __dir__. ' /‘. $class. " php ';}
Run test.php, page output:
Test1::testtest2::test
Later, the __autoload () method was discarded because a PHP project might rely on multiple frameworks, and if each frame had this function, it would report a fatal error that the function would repeatedly define. After the PHP 5.3 version, the official Spl_autoload_register ()was provided; Method instead of the __aotoload () method.
Spl_autoload_register () is characterized by the existence of multiple spl_autoload_register () methods.
Modify test.php:
<? phpspl_autoload_register (' autoload1 '); Test1::test (); Test2::Test (); function autoload1 ($class) { require __dir__. ' /‘. $class. " php ';}
The results of the page output are unchanged.
At this point, if multiple frames have their own classes loaded automatically, you only need to call multiple Spl_autoload_register (), each class will complete its own class automatically loaded, and will not conflict:
<? phpspl_autoload_register (' autoload1 '); Spl_autoload_register (' autoload2 '); Test1::test (); Test2::Test (); function autoload1 ($class) { require __dir__. ' /‘. $class. " php ';} function autoload2 ($class) { require __dir__. ' /‘. $class. " php ';}
The results of the page output are unchanged.
"Development tools": Phpstorm
"Programming fonts": Using equal-width fonts (common with courier View,consolas), it is recommended to use source Code Pro (Adobe company designed for programmers, free open source)
"Operating Environment": easyphp, integrated with Apache,mysql,phpmyadmin
PHP design Pattern Notes and summaries (1) Namespaces and classes Auto-loading