autoload and multiple directories

前端 未结 6 1624
醉酒成梦
醉酒成梦 2020-12-14 21:36

I\'ve just been looking at php\'s autoload() function. Seems a nice idea, but I\'m not sure how it handles multiple directories. My current development basically has a libra

6条回答
  •  清歌不尽
    2020-12-14 21:50

    You might want to take a look at the PEAR Convention for class names, which is really great for autoloading.

    Basically, it states that :

    The PEAR class hierarchy is also reflected in the class name, each level of the hierarchy separated with a single underscore.

    Which means finding the file to include for a classe name HTML_Upload_Error is just a matter of replacing '_' by '/' ; giving you HTML/Upload/Error.php

    For more explanations, and a couple of examples, you can take a look at the articles :

    • Autoloading PHP Classes to Reduce CPU Usage
    • Autoload Magic

    BTW : this convention is used by many big Frameworks / libraries ;-)
    For instance, Zend Framework uses this convention -- and it's really helpful !

提交回复
热议问题