自动加载和多个目录 [英] autoload and multiple directories

查看:74
本文介绍了自动加载和多个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看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.

基本上,它指出:

PEAR类的层次结构也是 反映在班级名称中,每个 用分隔的层次结构级别 一个下划线.

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

这意味着找到要包含类名称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 !

这篇关于自动加载和多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆