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($cla ss_name) {
include_once($c lass_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('AUTOLOA D_CLASS_DIR', '/classes/');
function __autoload($cla ss_name) {
require_once(AU TOLOAD_CLASS_DI R . $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.
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($cla ss_name) {
include_once($c lass_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('AUTOLOA D_CLASS_DIR', '/classes/');
function __autoload($cla ss_name) {
require_once(AU TOLOAD_CLASS_DI R . $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.
Comment