views:

184

answers:

5

Hi everybody,

Simple question : do you prefix your PHP classes (for example your library classes) with the name of your company ?

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 ?

+12  A: 

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.

John Feminella
+1: Avoid Hungarian Notation. http://en.wikipedia.org/wiki/Hungarian_notation
S.Lott
Prefixing classes is not a part of Hungarian notation.
Seva Alekseyev
Improper hungarian notation should be avoided. True hungarian, however, can be very helpful, especially in a language like PHP.
Andrew Noyes
Well namespaces seems the right solution, i'll test it in a small project.Thanks
Matthieu
@Andrew Noyes totally seconded. Stupid hungarian (say, `intWidth`) is stupid. Proper hungarian (say, `pxWidth`) can be a lifesaver.
Pekka
A: 

No. Although I did consider prefixing every class in my CMS with MCB, i.e.

class MCB_Admin
{
    // class contents...
}
$admin = new MCB_Admin;

However, I quickly decided against this and now just use standard class names.

Martin Bean
+3  A: 

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.

stereofrog
I agree 9001% about namespaces being ugly. But they're better than nothing.
John Feminella
5.2 is not a problem, there is no practical constraint, just a theorical question.
Matthieu
@John, better nothing than that "something"...
stereofrog
+1  A: 

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.

Arms
My company hosts its web application, no client server configuration to care about. No practical constaint.
Matthieu
Sometimes is the company in hosts the web applications and they do not intend to update :rolleyes:
ZJR
A: 

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

David Morrow
I used to do this, then I started dumping everything into a single directory... if I'm under 100 classes. Nothing angers me more than typing "cd" :)
Pestilence