To understand the design pattern first we need to understand the PHP namespace and the automatic loading of the class function
Let's take a look here.
Name space
Concept reason: such as a a.php article but we need two of the same directory at this time cannot exist two a.php then we can put in My/home and my/home1 directory, the namespace has its meaning
Then the space is my/home/a.php.
namespace My\name;
Demo
So here's the problem with automatic loading.
The simplest automatic loading is
function __autoload ($class) { $dir = './'; Set_include_path (Get_include_path (). Path_separator. $ids _dir); $class = str_replace (' \ ', '/', $class). '. php '; Require_once ($class); }
But with __autoload () This method is out.
Now we're using more of the Spl_autoload_register (advantage)
The interview may be designed to
Contrast:
1. Spl_autoload_register can make multiple write-registered load functions, and whoever registers first is called first, but _autoload () can only be called once
2.spl_autoload_register can catch errors
3. Since we can register then we can also use Spl_autoload_unregister () to cancel the registration.
Through the above study we can carry out a PSR schema processing
PSR Concept:
1. PSR-0 specification
[1] namespace must be consistent with absolute path
[2] class name first letter must be capitalized
[3] Except for the entry file, the other ". PHP" must have only one class
[4]php class files must be loaded automatically, not include, etc.
[5] Single inlet
2. Case
[1] directory structure
[2] Source code
index.php
!--? phpdefine (' Basedie ', __dir___); require_once ('/config/loader.php '); spl_autoload (' \ \ \ Config\\loader.php::autoload '); Config\object::test (); App\home\index::test (); Config/object.php
!--? phpnamespace config;class object{static function test () {echo "Nihao";}} Config/loader.php
!--? phpnamespace config;class loader{static function AutoLoad ($class) {require_once (Basedie .' /config/'. Str_replace (' \ \ ', '/', $class). php '); }} app/home/index.php
!--? phpnamespace app\home;class index{static func tion Test () {echo "Ceshixinxi";}}