PHP Class Auto-loading method _php Tutorial

Source: Internet
Author: User
Tags autoload php class php framework zend framework

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 ...

  • Related Article

    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.