Php class Automatic Loading Method

Source: Internet
Author: User

Php class Automatic Loading Method

Before PHP5, If PHP frameworks need to implement automatic class loading, they generally implement a traversal directory according to certain conventions and automatically load all classes or functions that comply with the agreed rules. Of course, PHP5's previous object-oriented support was not very good, and classes were not frequently used. Let's discuss it in detail.

Php 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

<? Php

Class inload

{

/**

* The class is automatically loaded and does not need to be called by the developer.

*

* @ Param string $ class file

*/

Private function autoload ($ class)

{

If (empty ($ class ))

{

Throw new QException ('loading file does not exist'. $ class );

}

Else

{

Require _ SPRING _. '/_ Core/SpringMap. php'; // framework Map

If (! File_exists ($ source [$ class] ['file'])

{

Throw new QException ('loading file does not exist'. $ class );

}

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

}

}

 

/**

* Register or unregister an automatic class Loading Method

*

* For more information about this method, see Zend Framework.

*

* @ Param string $ class: class that provides automatic loading Service

* @ Param boolean $ enabled or disabled this service

*/

Private function registerAutoload ($ class = 'interpreter', $ enabled = true)

{

If (! Function_exists ('spl _ autoload_register '))

{

Throw new QException ('spl _ autoload does not have 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 is all the content of this article. I hope you will like it.

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.