"Getting Started with PHP object-oriented (OOP) programming" 23. Auto-load Class __autoload () function

Source: Internet
Author: User
Tags autoload getting started with php

Many developers write object-oriented applications, creating a PHP source file for each class definition. A big annoyance is having to write a long list of included files at the beginning of each script (one file per class).

In a software development system, it is not possible to write all the classes in a PHP file, and when you need to invoke a class declared in another file in a PHP file, you need to introduce the file through include. However, sometimes, in a large number of projects, to each of the required classes of files are included in, is a very annoying thing, so we can use what class, and then the class where the PHP file import it? That's what we're going to talk about here. Auto-load class .

In PHP 5, you can define a __autoload () function that will be called automatically when trying to use a class that has not yet been defined, and by calling this function, the scripting engine has the last chance to load the required class before PHP fails, __autoload () The function receives a parameter that is the class name of the class you want to load, so when you do the project, you need to follow certain rules when organizing the name of the class, preferably with the class name as the center, or with a uniform prefix or suffix to form the file name, such as xxx_classname.php, classname_ Xxx.php and is classname.php and so on.

This example attempts to load the MyClass1 and MyClass2 classes from the myclass1.php and myclass2.php files, respectively

<?phpfunction __autoload ($classname) {require_once $classname. '. php ';} When the MyClass1 class does not exist, the __autoload () function is called automatically, passing in the parameter "MyClass1" $obj = new MyClass1 (),//myclass2 class does not exist, automatically calls the __autoload () function, passes in the parameter " MyClass2 "$obj 2 = new MyClass2 ();? >

The DiscuzX2.5 is handled in the following ways:

<?phpif (function_exists (' Spl_autoload_register ')) {spl_autoload_register (Array (' core ', ' autoload '));} Else{function __autoload ($class) {return core::autoload ($class);}}

Note:__autoload () is specifically designed for the non-existence of the Class!!! Many frameworks use this function to implement automatic loading of class files !!!

Attached: thinkphp Learning: The use of the Spl_autoload_register () function in PHP

"Getting Started with PHP object-oriented (OOP) programming" 23. Auto-load Class __autoload () function

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.