Php design mode preparation article to understand the design mode first, we need to first understand the php namespace and the automatic loading function of the class
Next let's talk about
Namespace
Concept: for example,. php article, but we need two files in the same directory at this time. php, we can put it in the my/home and my/home1 directories. at this time, the namespace has its meaning.
The space is my/home/a. php.
Namespace my \ name;
Demo
The following is the problem of automatic loading.
The simplest automatic loading method was
function __autoload($class) { $dir = ’./’; set_include_path(get_include_path().PATH_SEPARATOR.$ids_dir); $class = str_replace(’\’, ’/’, $class) . ’.php’; require_once($class); }
However, the _ autoload () method is out.
Now we use more spl_autoload_register (advantage)
The interview may be designed
Comparison:
1. Spl_autoload_register can perform multiple write registration and loading functions. whoever registers first calls the function first, but _ autoload () can only be called once.
2. spl_autoload_register can catch errors
3. you can also use spl_autoload_unregister () to cancel registration.
Through the above learning, we can handle a DSRs architecture.
This document introduces the following concepts:
1. Specification for PSR-0
[1] The namespace must be consistent with the absolute path
[2] The first letter of a class name must be in uppercase.
[3] except for the entry file, other ". php" must have only one class
[4] php files must be automatically loaded without using include
[5] Single Portal
2. case studies
[1] directory structure
[2] Source code
Index. php
Config/Object.php
Config/Loader.php
App/Home/Index.php