Two methods for php automatic loading

Source: Internet
Author: User
Php can be automatically loaded in two ways. For more information, see. Php supports two automatic loading methods.
The first scheme uses _ autoload, which is relatively simple and weak.
But there is a problem that cannot be solved, that is, to determine whether the file exists before include.
The code is as follows:
Set_include_path ('A'. PATH_SEPARATOR. get_include_path ());
Function _ autoload ($ className)
{
// If this check is added, because the file is not in the current directory, it will not detect the file,
// But the include operation is successful.
If (file_exists ($ className. '. php ')){
Include_once ($ className. '. php ');
} Else {
Exit ('No file ');
}
}
$ A = new Acls ();

The second method is to use spl for automatic loading.
Spl_autoload_register ()
A simple example
The code is as follows:
Set_include_path ('A'. PATH_SEPARATOR. get_include_path ());
// Function _ autoload ($ className)
//{
// If (file_exists ($ className. '. php ')){
// Include_once ($ className. '. php ');
//} Else {
// Exit ('No file ');
//}
/

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.