PHP Auto load autoload Mechanism sample sharing

Source: Internet
Author: User
Tags autoload eol

  This article mainly introduces the PHP automatic loading autoload Mechanism example, the file structure as follows, 2 ways to achieve automatic loading, the need for friends can refer to the following

1, custom functions   2,spl_autoload_register ()     code as follows: 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 19 11:28 /spl_autoload.php  /include:total drwxrwxr-x 2 Liuyuan liuyuan 4096 Feb-11:42./drwxrwxr-x 3 Liuyuan Liuy Uan 4096 Feb 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:     code as follows: <?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 ());?>     The results are as follows: The   code is as follows: liuyuan@ebuinfo:/var/www/phpgcs/ php_autoload$ php func_autoload.php  Array (    [0] =>/var/www/phpgcs/php_autoload/func_ autoload.php)  .:/ Usr/share/php:/usr/share/pear AClass is loaded Bclass is loaded Array (    [0] =>/VAR/WWW/PHPGCs/php_autoload/func_autoload.php     [1] =>/var/www/phpgcs/php_autoload/include/aclass.php     [2] =>/var/www/phpgcs/php_autoload/include/bclass.php)     The second way:   code as follows: <?php   &NBSP ; Class myloader{        public static function AutoLoad ($className) {        &NBSP ;   $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 in Spl_autoload_rEgister will fail because the AUTOLOAD_FUNC function pointer has pointed to the Spl_autoload method     * You can add the _autoload method to Autoload_functions list & nbsp   *    //spl_autoload_register (' __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?>     Running results are as follows: The code is as follows: liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ PHP spl_autoload. php  AClass is loaded Array (    [0] =>/var/www/phpgcs/php_autoload/spl_autoload.php     [1] =>/var/www/phpgcs/php_autoload/include/aclass.php)   Bclass is loaded  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.