PHP file naming conventions (.class, .inc)

14,284

Solution 1

Not really

Depending on how you have your .htaccess file set up, it can determine which classes are visible to the world. I believe best practice still says to end every file with .php if you can.

Solution 2

As far as the PHP interpreter is concerned there's no behavioural reason to include these descriptors at all. The de facto convention seem to be, to include them as part of the file suffix however I find it more useful to prefix them - i.e. my file names tend to look like:

class.*.php
inc.*.php
tpl.*.php

This is purely for organisational purposes; Whenever an application / terminal lists them in alphanumerical order each "type" will be grouped together. To conclude the question though it's really just down to preference, the only thing that's important - whatever you choose - is consistency.

Solution 3

I make my class files end with .class.php so I can see it's a class but no-one can view the source.

Solution 4

It is suggested by PHP best practice coding standards to name classes with class keyword somewhere in the class file name. However, the final decision is yours whether you want to stick with that or not. It has nothing to do with code execution.

Solution 5

In my opinion the best practice is to use a Framework and use the same naming conventions they uses in their sample projects. I don't think there is a Standard for it since it doesn't make a difference.

Most people name their classes as *.class.php and their static files as *.inc.php.

Share:
14,284
Brian
Author by

Brian

Updated on June 05, 2022

Comments

  • Brian
    Brian almost 2 years

    I see .class and .inc included in file names a lot of the time. My current understanding is that this is just a best practice to make the purpose/contents of the file clear.

    Is there any instance in PHP where a .class or .inc file name has a special purpose and truly means something?

  • arielnmz
    arielnmz over 9 years
    I don't get it, how are they not able to view the source?
  • Raghu Dodda
    Raghu Dodda about 6 years
    Most likely, he means to say that it is better to name a class foo.class.php instead of foo.class. In the second case, people can possibly retrieve /foo.class via the hosted web-server and see the code.