Framework Development (v)----Auto-load class

Source: Internet
Author: User

One. Why Auto Load

Automatic loading literally means no explanation. I am here to automatically load class parsing is the meaning of automatic loading of class files. The __autoload method is completely doable, but don't ask why, or use spl_autoload_register. Not only flexible, but also flexible, or flexible, reflected in __autoload can only be named this way, Spl_auto_register () can call different functions.

Principle of two automatic loading function

Typically, a class is defined as a file, and when classes and classes need to reference each other, the include (Require) class file is required, and the problem is that each time a class needs to be used, it is necessary to manually include (require). php5 Then there is the automatic loading mechanism of the class, which, if undefined, will load the required class before the error is thrown when the new class is __autoload. __autoload is the native function of PHP, Spl_auto_register is the C Extension functions. php and Python are scripts that are compiled into C to run.

**SPL Standard PHP Library.

* * Legend in the source code of PHP, spl_auto_load directly manipulate the pointer, so after this __autoload will not be used. Spl_auto_load priority is higher than __autoload.

Three pseudo code

In order to be lazy, design automatic loading function, take suffix name model,action,class, represent the database operation class respectively, respond to the operation class of foreground, third party introduce Lib class

Four implementations

Method is put in common.php.


1 /**2 * @ Auto load function Use3 * @ Various model functions, Class function naming rules4 * The @ naming rule must be XXL (l) Ibry | | XxC (c) Lass | | XXM (m) Odel5 * @ author [email protected]6  */7 functionMy_autoload ($class=NULL){8 //Var_dump ($class); exit ();9     $str=$class;Ten     $ext=substr($str,-5); One //Var_dump ($ext); A     $name=substr($class, 0,-5); - //Var_dump ($name); exit; -     $ext=Ucfirst(Strtolower($ext)); the //Var_dump ($name); exit (); - //Var_dump ($ext); Var_dump ($name); exit (); -     Switch($ext){ -          Case' Class ';//operating class for the foreground + //Echo App_path. ' Controller '. Directory_separator. $name. $ext. EXT; - //exit (); +             if(!file_exists(App_path. ' Controller '. Directory_separator.$name.$ext.EXT)) { A                 Exit(' class file does not exist '); at}Else{; -                 require_once(App_path. ' Controller '. Directory_separator.$name.$ext.EXT); -             } -              Break; -          Case' Model '://operation classes for the corresponding database -             if(!file_exists(App_path. ' Model '. Directory_separator.$name.$ext.EXT)) { in                 Exit(' model file does not exist '); -}Else{ to                 require_once(App_path. ' Model '. Directory_separator.$name.$ext.EXT); +             } -              Break; the          Case' Libry ';//corresponding third-party operation classes *             if(!file_exists(App_path. ' Libry '. Directory_separator.$name.$ext.EXT)) { $                 Exit(' library file does not exist ');Panax Notoginseng}Else{ -                 require_once(App_path. ' Libry '. Directory_separator.$name.$ext.EXT); the             } +              Break; A         default://there are no matches to the item the              if(!file_exists(App_path. ' Controller '. Directory_separator.$class.EXT)) { -  $                 Exit(' class file does not exist '); $}Else{  -                 require_once(App_path. ' Controller '. Directory_separator.$class.EXT); the             } -              Break;Wuyi}

The function is written, and is called before the first new one in the frame.

My code is in sina_router.php.

1 spl_autoload_register (' my_autoload ');

The preparation is complete.

Call it in the controller.

1      Public function result () {2         $obj New Pagelibry (); 3         Var_dump ($obj); 4     }

The browser effect is as follows.

That's all.

The next article is the cache class. Memcache Cache class.

This site blog are original, reproduced please indicate the source. Small three thanked ~~~! here.



Framework Development (v)----Auto-load class

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