Php5 _ autoload application and include performance comparison (1/2)-PHP source code

Source: Internet
Author: User
Ec (2); after php5, the _ autoload interceptor method is introduced to automatically include and reference class files. We usually write this: copy the Code as follows: function _ autoload ($ classname) {include_once $ classname .. class. php;} $ usernewuser (); when the php engine attempts to instantiate an operation of an unknown class, it calls script ec (2); script

After php5, the _ autoload interceptor method is introduced to automatically include and reference class files. We usually write this:
The Code is as follows:
Function _ autoload ($ classname ){
Include_once $ classname. '. class. php ';
}

$ User = new user ();

When the php engine tries to instantiate an operation of an unknown class, it will call the _ autoload () method, and there is a last chance to load the required class before the php error fails. Therefore, when the above code is executed, the php engine automatically executes the _ autoload Method for us to include the file user. class. php.

The exception thrown in the _ autoload function cannot be caught by the catch statement block and cause a fatal error.

If you use the cli interaction mode of php, the automatic loading mechanism will not be executed.

If you want to use pear-style naming rules, such as introducing the user/register. php file, you can do the following:
The Code is as follows:
// Load me
Function _ autoload ($ classname ){
$ File = str_replace ('_', directory_separator, $ classname );
Include_once $ file. 'php ';
}
$ Userregister = new user_register ();

Homepage 1 2 last

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.