Spl--spl_autoload_register

Source: Internet
Author: User
Tags spl

Spl_autoload_register () :

When a class is called Undefined, all functions registered to the Spl_autoload_register () function are called sequentially, instead of calling the __autoload function.

solve the problem :

The function __autoload ($classname) can be dynamically loaded classes, avoiding the use of require_once statements, but if you have a large-scale application that contains libraries or smaller applications, each application may want to declare a __ The AutoLoad function to find his file. The problem is that declaring two __autoload () functions at the global scope produces a duplicate declaration error.

Usage:

The SPL function accepts 3 parameters: [Add to the function of the auto-load stack, the loader cannot find the flag of the class when the exception is thrown = True, register to the team header instead of the end of the queue = false]

The first parameter is optional, which, by default, points to Spl_autoload (), which automatically finds files in the path that have a lowercase class name and a. php or. ini extension, or other extensions that are registered to the Spl_autoload_extensions () function.

<?PHPDefine(' Class_dir ', ' class/')/** Set_include_path Set the path of the PHP include file * Get_include_path gets the path of the current PHP include file, default.:/ usr/share/pear:/usr/share/php//linux to: Split, win with; split * Path_separator constant, which represents the delimiter of the current system*/Set_include_path(Get_include_path(). Path_separator. CLASS_DIR);//extension PHP contains file path//registration and returns the default file name extension used by the Spl_autoload function, this function is used to modify and examine the __autoload () function built-in default implementation function Spl_autoload () used by the extension, the default Spl_ The AutoLoad function uses a ". inc,.php" extension.   Spl_autoload_extensions ('. Class.php,inc,interface,.class '); Register multipleSpl_autoload_extensions ('. class.php '));//Default method//spl_autoload_register ();//Default Call, exception thrown by defaultSpl_autoload_register (NULL,false);//with default function, no exception is thrown when class is not foundSpl_autoload_register (' MyLoader1 ',false); Spl_autoload_register (' MyLoader2 ',false);//when instantiating a class, Spl_autoload () looks in the containing path first, where the class/ Test.class.php exists, will be include, if not present, will call myloader1,2 custom function, if still not found, throw a class exception that is not correctly declared. $instance=NewTest ();

Secure Call:

Once the Spl_autoload_register () is called, the __autoload () function in the program is not called back, and if you want to preserve the function of the __autoload (), register it as the first function in the auto-load stack:

if false = = = spl_autoload_function ()    ) {iffunction_exists(' __autoload ')) {        Spl_autoload_register (false);    }}

Spl_autoload_function (): Returns an array of registered functions and returns False if the SPL auto-load stack has not been initialized.

Spl--spl_autoload_register

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.