views:

318

answers:

4

I have seen many different naming schemes and extensions used for PHP files that are basically just classes. Some examples:

  1. myClass.php
  2. myClass.class.php
  3. myClass.class

What is the difference, and which is better?

+1  A: 

I prefer MyClass.php - the exact same name (including capitals) as the class.

However, if you are working on an existing project, you should use whatever naming scheme the project has.

Thomas Owens
+5  A: 

There is no difference beyond what you see. The ones with .class are helpful if you use autoloading and don't have a specific directory where your class files live. Otherwise, it's entirely arbitrary. (Personally, I use ClassName.class.php.)

chaos
thanks a trill...thats what i was lookin fo
johnnietheblack
I second that... ClassName.class.php.
James Skidmore
+5  A: 

What are your coding standards?

It’s common to start class names with a capital letter (for example: class MyClass). If you do this, then it follows that you would name the file MyClass.php. And MyClass.class.php works too.

MyClass.class is a bad idea that could allow someone to view your source code if they request that file by name. Using the .php extension ensures that the the user will only see the output after the file is processed by the PHP interpreter which—for a file that contains nothing but a class—is a blank page.

Finally, look into autoload(), which saves you the trouble of calling require_once in order to load your class.

Nate
Nate, i've heard of the autoload...is that stable? the php.net basically calls it a last ditch effort?
johnnietheblack
Plus one for the security issue being raised with MyClass.class
benlumley
I haven’t had problems with `__autoload`. The doc describes it as last chance, not last ditch. I don’t think that’s a bad thing.
Nate
A: 

Unless working on an existing project or platform, I tend to lean towards a documented set of conventions, such as Zend's: http://framework.zend.com/manual/en/coding-standard.naming-conventions.html

Plus this means you can use tools like PHP Code Sniffer to ensure everyone sticks to the convention.

Either way, Consistency is the name of the game. It makes it a lot easier for others (including yourself months from now) to get up to speed on a code base.

xentek