Php automatic loading class _ autoload () function _ PHP Tutorial

Source: Internet
Author: User
Php automatically loads the class _ autoload () function. Php automatically loads the class _ autoload () function. many developers create a PHP source file for each class definition when writing object-oriented applications. A major headache is that the class _ autoload () function has to be automatically loaded in every php script.

Many developers create a PHP source file for each class definition when writing object-oriented applications. A big headache is that you have to write a long list of contained files at the beginning of each script (each class has one file.

In a software development system, it is impossible to write all classes in one php file. when one php file needs to call the classes declared in another file, you need to introduce this file through include. However, when there are many file projects, it is a headache to include all the files of the required classes one by one, so can we use any classes, what about importing the php file where this class is located? This is what we want to talk about here.Automatic loading class.

In PHP 5, a _ autoload () function can be defined, which is automatically called when trying to use a class that has not been defined. by calling this function, before a PHP error fails, the script engine has the last chance to load the required class. a parameter received by the _ autoload () function is the class name of the class you want to load, therefore, when you create a project, you need to follow certain rules when organizing the file name of the class, it is best to center the class name, or you can add a uniform prefix or suffix to form a file name, for example, xxx_classname.php, classname_xxx.php, and classname. php and so on.

In this example, we try to load the MyClass1 and MyClass2 classes in the MyClass1.php and MyClass2.php files respectively.

 

The processing method of DiscuzX2.5 is:

 

 

 

Note: __autoload () is designed specifically for the absence of classes !!! Many frameworks use this function to automatically load class files !!!

Articles you may be interested in
  • How to set automatic phpmyadmin login and cancel automatic login
  • PHP checks browser parameters to prevent SQL injection.
  • Detailed usage of the jQuery plug-in (Lazy Load) for delayed image loading
  • Some powerful string processing functions forgotten by php
  • Php remembers the implementation of the next automatic password login
  • Thinkphp automatic verification and automatic filling are invalid solutions
  • PHP retrieves the complete url function of the current page, including parameters
  • PHP encryption and decryption functions

Many developers create a PHP source file for each class definition when writing object-oriented applications. A big headache is having to put it on every foot...

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.