AutoLoad () and Spl_autoload_register () functions in PHP

Source: Internet
Author: User
The __autoload () function can implement the required classes for automatic loading

Usage:

__autoload () When instantiating an object, the method is automatically called to load if no related file is introduced.

Instance:

Public Function __autoload ($className) {    $actionPath = "e://project/lib/action/". $className. ". Class.php ",    if (!file_exists ($actionPath))    {        echo $actionPath. Path does not exist ";    }    Require_once ($actionPath);}

Spl_autoload_register () function

Role: Registering a custom load function

For example, defining a loadfile () as a custom load function in a file, but after at least declaring or defining the function, when instantiating an object, the program does not automatically run the LoadFile () function, but automatically runs the __autoload () function. The Spl_autoload_register () function is to let the program automatically invoke the LoadFile () function when instantiating an object.

Instance:

 
  
 
  TestLoad ();? >

Result: Output: This is the TestLoad method in the test class;

Spl_autoload_register () has three parameters

The first: Array ($classname, $method), is an array with two elements, the first element represents the class where the auto-load method resides, and the second represents the function name of the auto-load method

Second parameter: Indicates whether an exception is thrown when the registration cannot be successful, True/false

The third parameter, True/false, indicates whether the function is registered to the top of the auto-load function queue.

Note: 1, spl_autoload_register () actually created the queue of the AutoLoad function, executed one by one in the order defined (so far I did not successfully implement one-by-one functions, please point out)

2, if the use of Spl_autoload_register () register a new automatic loading function, then the original __autoload () function will be invalidated, if you need to use the __autoload () function, you need to pass the Spl_autoload_register () To register the __autoload () function again, you can use this function

  • 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.