Php automatic loading class__autoload () method_php tutorial

Source: Internet
Author: User
Tags autoload
Php automatically loads class__autoload. This article will give you a brief introduction to the php automatic loading class__ autoload () method. I hope this method will be helpful to you. Purpose: when you need to instantiate a program, this article will give you a brief introduction to the php automatic loading class _ autoload () method, I hope this method will help you.

Purpose: when a class needs to be instantiated in the program, but this class is not in this file, you need to use the include function to include the external file. However, when there are many external classes to use, it will be very complicated to use the include function. this is the option to use the _ autoload () global function to automatically load the class.

When in index. to use the first three classes in php, you need to write three include ("name. class. php ") this method is very inefficient, but if you use the _ autoload () function, you don't have to worry about it. you just need to write this function method:

In PHP 5, this is no longer needed. You can define a _ autoload () function, which is automatically called when trying to use a class that has not been defined. By calling this function, the script engine has the last chance to load the required class before a PHP error fails.

In the following example, we will use an instance to explain how _ autoload () is used.


First, define a class ClassA named ClassA. class. php.

The code is as follows:

Class ClassA {

Public function funa (){

Echo "classA loaded successfully!
";

}

}

Then, define another class ClassB in the same directory. the file name is ClassB. class. php, and ClassB inherits ClassA.

The code is as follows:

Class ClassB extends ClassA {

Public function funb (){

Echo "classB also loaded successfully!
";

}

}

Finally, define an autoload. php file in the same directory as the above two files (this file name can be obtained randomly)

The code is as follows:

Function _ autoload ($ class_name ){

Require_once ("./". ucfirst ($ class_name). '. class. php'); // ucfirst converts the first letter of a class name to uppercase.

}

$ Obj = new ClassB ();

$ Obj-> funa ();

$ Obj-> funb ();

?>

Run the autoload. php program and check the result:

ClassA loaded successfully!

ClassB also loaded successfully!


Example: the folder contains the following files:

User. class. php;
Person. class. php;
Message. class. php;
Index. php;


We can do this.

The code is as follows:

Index. php

Function _ autoload ($ className) {// ucfirst () converts the first letter of the string to uppercase.

Include ("ucfirst ($ className)". class. ". php ");}

/**************

* For example, if the User class in User. class. php is instantiated in the index. php file, the _ autoload () function is automatically called if it does not exist.

The code is as follows:

* Pass the class name User as a parameter

*************/

$ User = new User ();

// Call the User. class. php file by automatically loading the class

$ Person = new Person ();

// Call the Person. class. php file by automatically loading the class

$ Message = new Message ();

// Call the Message. classphp file function = "" message = "new" person = "new" user = "new">

Explain () method. I hope this method will help you. Purpose: instantiate...

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.