How to automatically load PHP classes
Before PHP5, each PHP framework, if it was to implement the automatic loading of classes, would typically implement a class or function that iterates through the directory and automatically loads all files that conform to the Convention rules by some Convention. Of course, PHP5 's previous support for object-oriented is not very good, and the use of classes is not very frequent now. Let's go into the details.
PHP class Automatic Loading method
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 |
Class Inload
{
/**
* Class is automatically loaded, no need to be called by the developer
*
* @param string $class class file
*/
Private Function AutoLoad ($class)
{
if (empty ($class))
{
throw new Qexception (' Load file does not exist '. $class);
}
Else
{
Require _spring_. ' /_core/springmap.php '; Frame map
if (! file_exists ($source [$class] [' file '])
{
throw new Qexception (' Load file does not exist '. $class);
}
Require $source [$class] [' file '];
}
}
/**
* Register or unregister an automatic class loading method
*
* This method references the Zend Framework
*
* @param string $class classes that provide automatic onboarding services
* @param boolean $enabled Enable or disable the service
*/
Private Function registerautoload ($class = ' interpreter ', $enabled = True)
{
if (!function_exists (' Spl_autoload_register '))
{
throw new Qexception (' Spl_autoload does not exist for this PHP installation ');
}
if ($enabled = = = True)
{
Spl_autoload_register (Array ($class, ' autoload '));
}
Else
{
Spl_autoload_unregister (Array ($class, ' autoload '));
}
}
/**
* destructor
*/
Public Function __destruct ()
{
Self::registerautoload (' interpreter ', false);
}
|
The above mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/1010440.html www.bkjia.com true http://www.bkjia.com/PHPjc/1010440.html techarticle PHP class Automatic loading method before PHP5, each PHP framework if you want to implement the automatic loading of classes, generally according to some kind of convention to implement a traversal directory, automatically load all matching ...