PHP use keyword and file loading detailed _php tips

Source: Internet
Author: User
Tags autoload explode php framework

Objective

May be often used in the framework, write a Controller or Model , write a lot of use , but did not write the file loading code, think use can do the automatic loading of files.

Detailed Introduction

In fact, the current popular PHP framework, are based on the MVC pattern, a lot of the use of namespaces to improve the flexibility of the program. So how does the framework load the script file that corresponds to the class library that is declared by the USE keyword?

(1): The declaration use of the class library through the keyword declaration, the script will not be loaded, but in the script file is actually used to the corresponding class library will be loaded (this is called deferred loading).

(2): The framework generally in the initialization phase, it declared __ autoload() function or is a spl_autoload_register() function (usually in the entry file declaration). When a class library is invoked through a use declaration, the above function is invoked automatically, passing the called Class library name to the function (where the class library name is a full name that includes the namespace).

<?php
namespace Home\controller;
function __autoload ($class _name) { 
 //echo $class _name. " <br/> "; 
 Require_once ("./". $class _name. ". php");
Use Admin\controller\index;
New Index; The new index triggers the __autoload () function, returning Admin\controller\index to the function
?>

(3): the definition of __ function in a frame autoload() or a spl_autoload_register() function after receiving the name of the class library passed over, the framework handles the name, analyzes the file path corresponding to the class library name, and then loads the file. It should be noted here that different frameworks in the resolution of class library names, analysis of file paths are inconsistent, after all, the respective directory structure is not the same.

For example, thinkphp:

 /** * Class Library Auto Load * @param string $class object class name * @return void/public static function AutoLoad ($class) {//check whether to save
  In the map if (Isset (self::$_map[$class])) {include self::$_map[$class];
   }elseif (False!== Strpos ($class, ' \ \)) {$name = Strstr ($class, ' \ \ ', true); if ($name, Array (' In_array ', ' Org ', ' Behavior ', ' Com ', ' Vendor ')) | | Is_dir (lib_path. $name)) {//
   The namespace under the Library directory is automatically positioned $path = Lib_path;
    }else{//detection of custom namespaces otherwise the module is the namespace $namespace = C (' Autoload_namespace '); $path = Isset ($namespace [$name])? DirName ($namespace [$name]). '
   /': App_path; } $filename = $path. Str_replace (' \ \ ', '/', $class).
   EXT; if (Is_file ($filename)) {//WIN environment is strictly case-sensitive if (Is_win && false = = Strpos (str_replace ('/', ' \ \ \ '), Realpath ( $filename)), $class.
    EXT)) {return;
   Include $filename; }}elseif (! C (' App_use_namespace ')) {//Automatically loaded class library layer foreach (Explode (', ', C (' App_autoload_layer ')) as $layer) {if substr ($class, -strlen ($Layer) = = $layer) {if Require_cache (Module_path. $layer. /'. $class.
     EXT)) {return; An attempt was made to search for foreach (Explode (', ', ' C (' App_autoload_path ') as $path) {if (', ', ') ') {if ()}///////(') ' (', ', ') '
   . $class))//returns return if the Load class succeeds; }
  }
 }

Here is just an example of the use of the file with the automatic loading does not matter, do not emphasize how each frame is specific to complete the automatic loading, you can see the framework of the Code to study.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.