Do you prefix your classnames? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 11 months ago.
Improve this questionSimple question : do you prefix your PHP classes (for example your library classes) with the name of your comp开发者_JS百科any ?
Example : Zend prefix everything with "Zend_", but that's normal because this is a framework (so they want to avoid conflicts). Should I do the same for the library classes of my company ? Will I ever get conflicts ?
I think this is easier to use without the prefix (shorter names), but is that a good practice to have a prefix ?
PHP has namespaces. You should use them instead of prefixing everything with a name.
At the very least, your developers will thank you when they don't have to keep typing MyFrameworkName...
just to start using one of your classes.
I always prefix my classes for a number of reasons
- 5.2 will still be around for a couple of years
- 5.3 namespaces are totally ugly, i cannot use them without getting nauseous
- and hey, apple does that too (NSString, CFXMLParser etc) ;))
Seriously, it's better to avoid explicit class names altogether. Get rid of statics and use dependency injection instead of new
- and you'll have much less pain with class naming.
Yes, I do. I can't switch to namespaces b/c most of my clients are running PHP 5.3 yet. I don't mind typing a little more if I'm guaranteed to avoid naming conflicts. Especially when I'm using my own custom library on top of existing ones.
No.
PHP has namespacing, so you can have classes with the same name so long as they’re in different namespaces.
i prefix my classnames with the folder name it lives is in
<?php
// filename would be "ClassName.php" in directory "directory"
class directory_ClassName{
}
that way it make using __autoLoad extremely easy
function __autoload( $class_name ){
$file = explode( "_", $class_name );
$file = 'app/' . implode( '/', $file ) . ".php";
require_once( $file );
}
meaning you never have to load your classes, its all done automagicly
精彩评论