Tutorial :What about file-path-in-class-name convention?


What do you (especially PHP) guys think about a naming convention for classes in which the class name reflects the path to the file, related to the project directory? e.g:

# /project/Session/Abstract.php  # /project/Session/Database.php    class Session_Database extends Session_Abstract ...  

I'm sure you get the idea. I'm also sure some of the pros are obvious.. but what do you think - is it worth it to lengthen class names in order to get pretty nice directory structure which is easy to navigate?

This also allows for a one-liner __autoload( $class ) definition in PHP: str_replace( '_', '/', $class );

I suppose some people will consider such convention to be stupid. I personally like it, but I haven't seen it in use by other people and I am not quite sure if it will work so well in practice.

One of the cons might be that with the removal of include/require calls, all classes are dependent(glued together) on the autoload function, which some might argue, does not comply with their understanding of loose coupling.

The only reference that is known to me so far about such approach is http://softwareengineering.vazexqi.com/files/pattern.html

So, do you have an opinion on this one?


Then you haven't been looking. PEAR uses this approach. So does Zend Framework. It's a rather established standard in the PHP world. I don't know when I first encountered it, but I've personally used it and liked it for several years, both as part of my own framework and as part of publicly available frameworks such as ZF.


The only real thing that I don't like about this is that when I need to move code around, I end up re-working lots of names. However, Emil has already pointed out that this is pretty much par for the course, but I'd like to add the PHP Userland Naming Rules.


I use that convention aswell.

Not only does it help have a pretty file structure, it also helps with namespacing issues. That naming convention helps prefixing classes to avoid duplicate class names.

When PHP 5.3 comes out and finally has namespaces, i'll still be using this convention. The only thing I'll do differently is shorten my class names, convert my prefixes into proper namespaces and do adjustment in my autoloader.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »