PHP-based Autoload

Source: Internet
Author: User

PHP automatic loading autoload mechanism is very important, here do 2 small exercises original article, reprint Please note: http://www.cnblogs.com/phpgcs file structure is as follows, 2 ways to achieve automatic loading 1, custom function 2, spl_autoload_register () liuyuan @ ebuinfo:/var/www/phpgcs/php_autoload $ ll. /*-rw-r -- 1 liuyuan 800 Feb 19 11: 39. /func_autoload.php-rw-rw-r -- 1 liuyuan 906 Feb 19. /spl_autoload.php. /include: total 16drwxrwxr-x 2 liuyuan 4096 Feb 19 11: 42. /drwxrwxr-x 3 liuyuan 4096 Feb 19 .. /-rw-r -- 1 liuyuan 142 Feb 19 11: 42 aClass. php-rw-r -- 1 liuyuan 143 Feb 19 11: 42 bClass. php first looks at the User-Defined Function Method: <? 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 /'. $ classN Ame. '. 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 running result is as follows: + View Code Method 2: <? Php class myLoader {public static function autoload ($ className) {$ filename = '. /include /'. $ className. '. php '; if (file_exists ($ filename) {include_once $ filename;} else {exit ('no file') ;}} define ('eol ', (PHP_SAPI = 'cli ')? PHP_EOL: '<br/>'); spl_autoload_register (array ('myloader ', 'autoload');/*** _ autoload method will expire after spl_autoload_register, because the autoload_func function pointer has pointed to the spl_autoload Method *, you can use the following method to add the _ autoload method to the autoload_functions list * // 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;?>

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.