dllregisterserver PHP function spl_autoload_register usage and __autoload introduction

WBOY
Release: 2016-07-29 08:47:54
Original
1092 people have browsed it

I won’t talk about the usage of __autoload() anymore, I have mentioned it before in my WEB development notes. How to use PHP __autoload function (automatically load class files), original address: http://www.jb51.net/article/29625.htm.
Let’s talk about the usage of spl_autoload_register(). It’s very simple. It can be understood like this, which is to declare a custom __autoload(). You can be an A function or a B function. You can do whatever you want. Function body Of course, the writing method must be the same as __autoload().
This method will be called when PHP cannot find the class file. When registering its own function or method, PHP will not call the __autoload() function, but will call the custom function
spl_autoload_register('func_name');
spl_autoload_register (array('class_name','method_name'));
Details are as follows:
spl_autoload_register
(PHP 5>= 5.1.2)
spl_autoload_register — Register __autoload() function
Description
bool spl_autoload_register ([ callback $autoload_function ] )
Register the function into the SPL __autoload function stack. Activate functions in this stack if they are not already active.
If the __autoload function has been implemented in your program, it must be explicitly registered in the __autoload stack. Because the
spl_autoload_register() function will replace the __autoload function in Zend Engine with spl_autoload() or
spl_autoload_call().
Parameter
autoload_function
The autoload function to be registered. If no parameters are provided, the default implementation function of autoload
spl_autoload() is automatically registered.
Return value
Returns TRUE if successful, FALSE if failed.
Note: SPL is the abbreviation of Standard PHP Library. It is an extension library introduced in PHP5. Its main functions include the implementation of the autoload mechanism and various Iterator interfaces or classes. The SPL autoload mechanism is implemented by pointing the function pointer autoload_func to a self-implemented function with autoloading function. SPL has two different functions spl_autoload and spl_autoload_call. Different automatic loading mechanisms are implemented by pointing autoload_func to these two different function addresses.
The code is as follows:
test.class.php

Copy the code The code is as follows:


class abc{
function __construct()
{
echo 'www.chhua.com;
}
}
?>


load.php

Copy code The code is as follows:


class LOAD
{
static function loadClass($class_name)
{
$file name = $ class_name.".class.php";
if (is_file($filename)) return include_once $filename;
}
}
/**
* Set up the automatic loading of objects
* spl_autoload_register — Register given function as __autoload() implementation
*/
spl_autoload_register(array('LOAD', 'loadClass')) ;
$a = new Test();//Achieve automatic loading. Many frameworks use this method to automatically load classes
?>

The above introduces the usage of dllregisterserver PHP function spl_autoload_register and the introduction of __autoload, including the content of dllregisterserver. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!