Follow Sinsing to know PHP's own active loading

Source: Internet
Author: User
Tags function prototype

As a framework, the file loading mechanism is not less, then we should how to load it, these PHP has been given to us, so we just need to follow the rules to be able to do, PHP has two functions to complete this function, the first is __autoload, is now very little use, Instead of a more powerful spl_autoload_register, the version number required is the version number after PHP5.2.1, and now our mainstream version number is supported, so it's safe to use.

In the previous PHP version number, we often need to require a bunch of file names, such as configuration files, database connection files, log files, and so on, and then we have the __autoload function, it is in a function completed all the import work, it has a strong advantage is to be able to deal with the programmatic way , for example, using the normal table to import n files at a time, to a certain extent, it will be very convenient for our operation, but, we must in the __autoload all the types are written to, or the initiative to download the membership is not very good implementation.

Now with Spl_autoload_register, its function is to be able to register n loading functions, we can use this built-in function to specify the name of the file we need to load, and here is its function prototype:

BOOL Spl_autoload_register ([callback $autoload _function])
Below we use the code to combat a bit:

First we create a b.class.php, with the following content such as:

<?phpclass B {Private $name = "Sinsing";p rivate $age = 23;function getmsg () {return "name is:". $this->name. "   Age is: ". $this->age;}}
We then create a new a.php file under the sibling folder, such as the following:

<?php//first defines a load function xin_load ($class) {$file = $class. ". Class.php "; if (file_exists ($file)) {require_once ($file);}} Then register it spl_autoload_register (' xin_load '); $class = new B (); Echo $class->getmsg ();
The reason why Class B here can be properly instantiated is that when we define an instance of Class B, the system discovers the need to load the file, so he finds all the loading functions, Then b.class.php found in the definition of B, also loaded in the file, it is worth noting that there are only b.class.php are imported, the other formats almost identical will not be imported.





Follow Sinsing to know PHP's own active loading

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.