Home > Backend Development > PHP Tutorial > PHP class automatic loading method_PHP tutorial

PHP class automatic loading method_PHP tutorial

WBOY
Release: 2016-07-13 09:52:02
Original
699 people have browsed it

PHP class automatic loading method

Before PHP5, if each PHP framework wanted to implement automatic loading of classes, they usually implemented a directory traversal according to a certain agreement. Automatically load classes or functions of all files that meet the agreed rules. Of course, the object-oriented support before PHP5 was not very good, and the use of classes was not as frequent as it is now. Let’s discuss this in detail.

 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

21

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

{

/**

* 类自动载入,不需要由开发者调用

*

* @param string $class 类文件

*/

private function autoload( $class )

{

if( empty($class) )

{

throw new QException('加载文件不存在'.$class);

}

else

{

require _SPRING_.'/_Core/SpringMap.php'; //框架地图

if(! file_exists( $source[$class]['file'] ) )

{

throw new QException('加载文件不存在'.$class);

}

require $source[$class]['file'];

}

}

 

/**

* 注册或取消注册一个自动类载入方法

*

* 该方法参考 Zend Framework

*

* @param string $class 提供自动载入服务的类

* @param boolean $enabled 启用或禁用该服务

*/

private function registerAutoload($class = 'Interpreter' , $enabled = true)

{

if (!function_exists('spl_autoload_register'))

{

throw new QException('spl_autoload 不存在这个PHP的安装');

}

if ($enabled === true)

{

spl_autoload_register(array($class, 'autoload'));

}

else

{

spl_autoload_unregister(array($class, 'autoload'));

}

}

 

/**

* 析构函数

*/

public function __destruct()

{

self::registerAutoload('Interpreter' , false);

}

1

2 3

45 6 7 8 9 10
11
12
13 14 15 16 17 18 19 20 21 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
The above is the entire content of this article, I hope you all like it. http://www.bkjia.com/PHPjc/1010440.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1010440.htmlTechArticlephp The method of automatic loading of classes Before PHP5, if each PHP framework wanted to implement automatic loading of classes, generally It is to implement a directory traversal according to a certain convention and automatically load all items that match...
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