Heim  >  Artikel  >  php教程  >  php 类自动载入的方法

php 类自动载入的方法

WBOY
WBOYOriginal
2016-06-13 09:02:501130Durchsuche

php 类自动载入的方法

   在PHP5之前,各个PHP框架如果要实现类的自动加载,一般都是按照某种约定自己实现一个遍历目录,自动加载所有符合约定规则的文件的类或函数。 当然,PHP5之前对面向对象的支持并不是太好,类的使用也没有现在频繁。 我们来详细探讨下吧。

  php 类自动载入方法

  ?

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);

}

  以上所述就是本文的全部内容了,希望大家能够喜欢。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn