PHP's Spl_autoload Standard library approach

Source: Internet
Author: User

The Spl_autoload method in PHP5 is equivalent to implementing its own __autoload

<? PHP     function __autoload ($classname) {        if(is_file($classname . '. php ') {            include$classname. php ';         ElseIf (is_file($classname. Inc ') {            include$classname. Inc ';        }    }

It will automatically look for a. php/.inc file with the same name as $classname in the registration directory. Of course, you can also specify a specific file by registering the extension

<? php    spl_autoload_extensions ('. Php,.inc,.some ');

So how do you load Spl_autoload automatically, by

<? php    spl_autoload_register ();

Spl_autoload_register has a $callback parameter, if not specified, it will automatically register spl_autoload, in order to be able to search for more automatic loading directory, you can set the automatic loading directory before the code

<? PHP     Set_include_path (Get_include_path(). Path_separator. ' Some/path '. Directory_separator);

These methods are commonly used in PHP frameworks.

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.