PHP autoload Automatic loading mechanism usage example

Source: Internet
Author: User
Since PHP5, introduced the AutoLoad this interceptor method, can automatically carry on the class file to contain the reference, below to see the concrete use method.

In the process of PHP development, if you want to introduce a class from the outside, you will typically use the include and require methods to include the file that defines the class, but this may make the new script that references the file There are a large number of include or require method calls, which can cause errors if inadvertently omitted, making the code difficult to maintain.

Since PHP5, the AutoLoad method has been introduced to automatically include references to class files, which we typically write:

function AutoLoad ($className) {include_once $className. '. class.php '; } $user = new user ();

When the PHP engine attempts to instantiate an operation of an unknown class, it calls the AutoLoad () method, which has the last chance to load the required class before the PHP error fails. So, when the above code executes, the PHP engine actually automatically executes a autoload method for us, including the User.class.php file.

Exceptions thrown in the AutoLoad function cannot be caught by a catch statement block and cause a fatal error.

If you use PHP's CLI interaction mode, the auto-load mechanism will not execute.

This can be done when you want to use pear-style naming conventions, such as the need to introduce user/register.php files:

Load my function AutoLoad ($className) {$file = Str_replace (' _ ', Directory_separator, $className); Include_once $file. ' PHP '; } $userRegister = new User_register ();

This method is convenient, but in a large-scale application, if you introduce more than one class library, there may be some puzzling problems due to the autoload mechanism of different class libraries. After PHP5 introduced the SPL standard library, we have another new solution, the Spl_autoload_register () function.

The function is to register the function in the AutoLoad function stack of SPL and remove the system default AutoLoad () function. Once the Spl_autoload_register () function is called, all functions registered to the Spl_autoload_register () function are called sequentially, rather than automatically called by the AutoLoad () function when the class is not defined. The following example calls user/register.php instead of User_Register.class.php:

Copy the Code code as follows:

Do not load my function autoload ($className) {include_once $className. '. class.php '; }//Load my function AutoLoad ($className) {$file = Str_replace ('/', Directory_separator, $className); Include_once $file. '. php '; }//Start loading Spl_autoload_register (' autoload '); $userRegister = new User_register ();

When using Spl_autoload_register (), we can also consider a more secure initialization invocation method, as follows:

The code is as follows:

System default AutoLoad function AutoLoad ($className) {include_once $className. '. class.php '; }//AutoLoad function AutoLoad ($className) {$file = Str_replace (' _ ', Directory_separator, $className) available for SPL loading; Include_once $file. '. php '; }//accidentally loaded the wrong function name, but also the default autoload mechanism to cancel ... Embarrassed Spl_autoload_register (' _autoload ', false); The fault tolerance mechanism if (false = = = Spl_autoload_functions ()) {if (function_exists (' AutoLoad ')) {spl_autoload_register (' autoload '), FALSE); } }

In the Unix/linux environment, if you have multiple smaller classes, for ease of management, are written in a PHP file, you can use the Ln-s command as a soft link to quickly distribute multiple copies of different class names, and then through the autoload mechanism to load.

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.