The AutoLoad of PHP basics

Source: Internet
Author: User
Tags autoload define exit eol include php basics
PHP automatic loading autoload mechanism is very important, here do 2 small exercises   original articles, reproduced please note: Http://www.cnblogs.com/phpgcs   file structure as follows, 2 ways to implement automatic loading   1, custom functions   2,spl_autoload_register ()     liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ LL./*-rw-rw-r-- 1 Liuyuan Liuyuan  800 Feb 11:39./func_autoload.php-rw-rw-r--1 Liuyuan Liuyuan  906 Feb. 11:28 oload.php  /include:total drwxrwxr-x 2 Liuyuan liuyuan 4096 Feb-11:42./drwxrwxr-x 3 Liuyuan Liuyuan 4096 F EB 19 11:43. -rw-rw-r--1 Liuyuan Liuyuan  142 Feb 11:42 aclass.php-rw-rw-r--1 Liuyuan Liuyuan  143 Feb-11:42 BClas s.php   First look at the Custom Function mode:   <?php     define (' EOL ', (php_sapi = ' cli ')? Php_eol: ' </br> ');     Print_r (Get_included_files ());     Echo EOL;     Print get_include_path ();     Echo EOL;    //set_include_path (Get_include_path (). Path_separator. ' /var/www/ly_php/php_spl/include/');    //set_include_patH (dirname (__file__). /include ');    //set_include_path (dirname (__file__). /include/');       function __autoload ($className) {        $filename = './include/'. $className. ' PHP ';        //$filename = './include/'. $className. PHP ';        //$filename = '/var/www/ly_php/php_spl/include/'. $className. PHP ';         if (file_exists ($filename)) {            include_once $filename;        }else{            exit (' no file ');         }    }       $a = new AClass ();     $b = new Bclass ();     Print_r (Get_included_files ())?> running results are as follows:   + View Code       The second way:   &NBS P <?php     class myloader{        public static function AutoLoad ($className) {  &NB Sp         $filename = './include/'. $className. PHP ';             if (file_exists ($filename)) {              &NB Sp Include_once $filename;            }else{                exit (' no file ');                     {   }       define (' EOL ', (Php_sapi = = ' cli ')? Php_eol: ' <br/> ');       Spl_autoload_register (Array (' Myloader ', ' autoload '));      /**     *__autoload method will fail after spl_autoload_register because the AUTOLOAD_FUNC function pointer has pointed to the Spl_autoload method     * You can add the _autoload method to the Autoload_functions list    /   //spl_autoload_register in the following ways (' __autoload ');         error_reporting (e_all^e_notice^e_warning^e_error);     error_reporting (E_notice e_warning);       $a = new ACLASS ();     Print_r (Get_included_files ());     Echo EOL;     $b = new Bclass ();        echo EOL;?>

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.