PHP design Mode Preparation Chapter

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

  • 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.