PHP Auto Load Class

Source: Internet
Author: User
Tags autoload class definition functions php file php and php error mysql database

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 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 all the required class files included in, is a very frustrating thing, so we can not use what kind of time, and then this class in the PHP file to 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 is invoked automatically when an attempt is made to use a class that has not yet been defined, and by calling this function, the script engine has the last chance to load the required class before the PHP error fails, __autoload () The function receives a parameter, is the class name that you want to load, so when you do the project, when the organization defines the name of the class, you need to follow certain rules, preferably the class name as the center, or you can add a unified prefix or suffix to form the filename, such as xxx_classname.php, classname_ Xxx.php and just classname.php and so on.

This example attempts to load the MYCLASS1 and MYCLASS2 classes separately from the myclass1.php and myclass2.php files

function __autoload ($classname)
	require_once $classname. '. php ';

When the MyClass1 class does not exist, the __autoload () function is invoked automatically, passing in the argument "MyClass1"
$obj = new MyClass1 ();

When the MyClass2 class does not exist, the __autoload () function is invoked automatically, passing in the argument "MyClass2"
$obj 2 = new MyClass2 ();
? >

DiscuzX2.5 is handled in the following ways:

if (function_exists (' Spl_autoload_register '))
	spl_autoload_register array (' Core ', ' AutoLoad '));
	function __autoload ($class)
		core::autoload ($class);

Note:__autoload () is specifically designed for the absence of a class!!! Many frameworks use this function to implement automatic loading of class files!!!

Articles that you may be interested in

    • How to set up phpMyAdmin automatic login and cancel automatic logon
    • Some powerful string-handling functions that PHP has forgotten
    • The JQuery plugin (Lazy load) Usage details for deferred loading pictures
    • PHP Remember the next time the password automatically login implementation method
    • PHP Check browser parameters to prevent SQL injected functions
    • PHP uses the Session_set_save_handler () function to save session to MySQL database
    • PHP converts Arabic numerals into Chinese character functions
    • PHP gets the full URL address function of the current page, including parameters

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: 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.