灵活的__autoload计划 [英] Flexible __autoload scheme
问题描述
随着PHP5的出现,以及它的OO支持和新的__autoload
功能,我正在考虑实施一些计划之王来组织这样一个
类。它们可以通过__autoload
函数轻松找到,但同时也可以清楚地组织课程。
目前(在PHP4中)我'使用类似Java的方案,借用ActiveLink
IORCA( www.active-link.com),它使用特殊的import()函数和
类在文件和目录中组织。每次我需要某个
类时,我必须手动导入相应的类或包。这很好,直观地特别适合那些有Java经验的人,但是作为
PHP5为我们提供了一个很好的功能,如__autoload函数,我会很多
更喜欢利用它。
对于那些不知道的人来说,__ autoload是一个神奇的功能,叫做
每个时间一个先前未定义的类,被调用的类名称作为参数传递给
。 Andi和Zeev在他们即将出版的书中(参见
http ://www.zend.com/php5/andi-book-excerpt.php )给出最简单的例子:
函数__autoload($ class_name){
include_once($ class_name。" php");
}
但是,这会导致单个类文件混乱目录,和
整个系统需要更多的组织。首先,我将
至少将上述代码更改为:
define(''AUTOLOAD_CLASS_DIR'',''/ classes /'') ;
函数__autoload($ class_name){
require_once(AUTOLOAD_CLASS_DIR。$ class_name。" .class.php");
}
这会给我一个中心类目录,其中存储我所有的类
,以及一个不同的扩展名(class.php)对于类文件;
现在还需要类文件而不是包含。但是,它仍然有点简陋:如果我想按项目组织我的课程,
包等怎么办?
我正在考虑一个系统,其中所有的class.php文件都将在目录中组织
,__ autoload函数将以某种方式找到正确的
。
遍历所有目录可能会太慢,
,特别是在大型类层次结构中。一种方法是__autoload
包含一个定义数组的文件,该数组有效地镜像文件系统
结构。这意味着我们每次添加(或
移动)一个类时都需要编辑数组,或者实现一种机制,如果一个类
isn''发现,猛扑过目录并重新创建结构。
另一个缺点仍然存在,即每个类仍然需要
必须有一个不同的名称才能识别由系统。
这看起来似乎可能有用(我仍然需要写它),但是我很好b / b
好奇如果有人有一些不同最好更好地实现
这个。
Berislav
-
如果互联网是马克斯兄弟电影,网络,电子邮件和IRC是Groucho,Chico和Harpo,那么Usenet就是Zeppo。
With the advent of PHP5, with its OO support and the new __autoload
function, I was thinking of implementing some king of scheme to organize
classes in such a way that they can be easily found by the __autoload
function, yet at the same time would have clear organization of classes.
Currently (in PHP4) I''m using a Java-like scheme, borrowed from ActiveLink
IORCA (www.active-link.com), which uses a special import() function and
classes organized in files and directories. Each time I need a certain
class, I must manually import the appropriate class or package. This works
nicely, and intuitively especially for those with experience in Java, but as
PHP5 offers us such a nice feature as the __autoload function, I would much
prefer to take advantag of it.
For those who don''t know, __autoload is a magical function that is called
each time a previously undefined class, with the called class name passed as
the argument. Andi and Zeev in their upcoming book (see
http://www.zend.com/php5/andi-book-excerpt.php) give the simplest example:
function __autoload($class_name) {
include_once($class_name . "php");
}
However, this leads to a clutter of class files in a single directory, and
the whole system calls for a little more organization. First of all, I would
at least change the above code to:
define(''AUTOLOAD_CLASS_DIR'', ''/classes/'');
function __autoload($class_name) {
require_once(AUTOLOAD_CLASS_DIR . $class_name . ".class.php");
}
This would give me a single central classes directory where all my classes
are stored, as well as a distinct extension (class.php) for class files;
also the class file is now required instead of included. However, it is
still a bit spartan: what if I want to organize my classes by projects,
package etc?
I was thinking of a system where all the class.php files would be organized
in directories, and the __autoload function would somehow be able to find
the right one.
Going through all the directories would potentionally be too slow,
especially in a large classes hierarchy. One approach is that the __autoload
includes a file defining an array which effectively mirrors the filesystem
structure. This means that we need to edit the array each time we add (or
move) a class, or perhaps implementing a mechanism that would, if a class
isn''t found, swoop through the directories and recreate the structure.
Another downside would still remain, which is that each class would still
have to have a different name to be recognized by the system.
This seems like it might work (I still have to write it, though), but I am
curious if anyone has some different and preferably better idea to implement
this.
Berislav
--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.
推荐答案
class_name){
include_once(
class_name) {
include_once(
class_name。" php");
}
然而,这会导致混乱在单个目录中的类文件,以及整个系统需要更多组织的
。首先,我将
至少将上述代码更改为:
define(''AUTOLOAD_CLASS_DIR'',''/ classes /'') ;
函数__autoload(
class_name . "php");
}
However, this leads to a clutter of class files in a single directory, and
the whole system calls for a little more organization. First of all, I would
at least change the above code to:
define(''AUTOLOAD_CLASS_DIR'', ''/classes/'');
function __autoload(
class_name){
require_once(AUTOLOAD_CLASS_DIR。
class_name) {
require_once(AUTOLOAD_CLASS_DIR .
这篇关于灵活的__autoload计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!