Php design pattern preparation

Source: Internet
Author: User
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

    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.