问题描述
我一直在看php的autoload()函数.似乎是个好主意,但我不确定它如何处理多个目录.我当前的开发基本上具有库目录结构,该库目录结构通过操作将类分组为子目录.我想知道我必须为每个目录声明一个include()……我真的希望我不必这样做.
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 library directory structure grouping classes into subdirectories by operation. I'm wondering I have to declare a include() for each directory ... which I really hope I don't have to do.
你能建议-谢谢
推荐答案
您可能想看看 PEAR约定用于类名,这对于自动加载确实非常有用.
You might want to take a look at the PEAR Convention for class names, which is really great for autoloading.
基本上,它指出:
这意味着找到要包含类名称HTML_Upload_Error
的文件仅是将'_'替换为'/'的问题;给你HTML/Upload/Error.php
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:许多大型框架/库都使用此约定;-)
例如,Zend Framework使用此约定-确实很有帮助!
BTW : this convention is used by many big Frameworks / libraries ;-)
For instance, Zend Framework uses this convention -- and it's really helpful !
这篇关于自动加载和多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!