Take the 2-minute tour ×

I really like using ClassNames and memberNames as convention but I am not sure how I would name the file containing a class.

I like making my classes defined in a file with the exact same name as the class. But I also like making php files all lowercase. So I am conflicted.

If I have a class called ProductGroup should that be defined in ProductGroup.php, productgroup.php, or product_group.php?

I know there is no right answer, so I am looking for what is most common or your opinion of which to use.. which do you use?

share|improve this question
add comment

6 Answers

At work we use underscores as folder delimiters and name the files exactly the same as the path. The autoloader is very simple, it just has to replace _ with / and add '.php' to the end. ProjectName_Models_ProductGroup() will always reside in ProjectName/Models/ProductGroup.php. It can make for some very long class names, but it doesn't really matter with an IDE.

You could use the same convention, but just run strtolower() before including the file.

share|improve this answer
+1 This is the convention adopted by Zend Framework and seems to be gaining wide acceptance. –  bogeymin Oct 11 '10 at 14:56
do you mean you name the class exactly the same as the file path? –  systemovich Nov 11 '10 at 7:12
@Geoffery, Yes it is exactly the same except slashes are replaced with underscores. All of our servers run under Linux so capitalization matters and it makes for a very simple autoloader. –  Asa Ayers Nov 13 '10 at 2:57
add comment

I would typically put a 'ProductGroup' class into a file named 'ProductGroupClass.php', typically stored in a 'classes' directory structure.

share|improve this answer
I think that is overkill. Appending class to a file in a directory called classes seems too redundant –  Matt Ellen Oct 11 '10 at 13:41
add comment

I try to name my files the same as the class name but in lower case. This helps out with autoload functions as well.

share|improve this answer
add comment

I have class files that span applications ("libraries"), and class files specific to applications. The libraries get a prefix to minimize the potential for conflicts with any third-party code we might include, ie. at "My Sweet Company" we might use the class name "MSCPerson" rather than "Person."

We use CamelCase class names, and class files are named CamelCase.class.php. Most class files have the single class and any class-specific exceptions, but this rule is not set in stone. It's nice to have single-class files for vim's gf feature.

share|improve this answer
add comment

Personally, I store a class named MyClass in a file called MyClass.class.php, inside a classes directory.
My autoload function is then as simple as

function __autoload($class) {

There is no structure inside the classes directory, but in my projects this is never been a problem. If you need to have subdirectories, the method suggested by Asa Ayers is very practical and easy to implement.

share|improve this answer
add comment

I use a modified version of PEAR Coding Standards. As mentioned before it helps with autoloading, and I find it looks a little cleaner and easier to read IMO. Basically what it comes down to is just decide what works for you and stick to it. As long as all the code looks the same, in the end the rest will fall into place.

share|improve this answer
add comment

Your Answer


