PHP includes advanced file extension methods

Firstly, I admit that I am anal about such things. (Bad, bad, me.) However, I'm just wondering what is considered best practice in terms of naming files with PHP.

As a basic option, I am going to save .php as the final extension (to prevent unreviewed files from being extracted), but to help distinguish between the front-end file and the include file, I'm either

  • Name all files include XXX.inc.php

  • Name the common (non-classical) files as mentioned above and the class definitions as ClassName.class.php (Potentially convenient for using the autoloader on line, although I'm not a big fan of autoloaders.)

I'm currently moving on to option 2, but I'm just wondering if there are any other suggestions or recommendations that you would recommend.

+7
include php frameworks
source share
2 answers

First of all, I completely agree with you when you say that all PHP files should have ".php" as the final extension; Two reasons for this:

  • as you stated, this helps prevent the extraction of unreviewed files.
  • it also helps with IDEs / editors that do syntax coloring based on the file name: you do not need to configure it to treat ".inc" as a PHP file.

There are times when I do otherwise; the main reason for this is that I use a tool (CMS, Framerwork, library, ...) that has some rules about file names: I try to follow them even if I don't like them.

For example:

  • With Drupal, I use ".inc", ".module", ".install", ...
  • With the Zend Framework, I use ".phtml" for view scripts (HTML + PHP)

For files containing classes, I don't like ".class.php": I think this is a bit redundant; I use "MyClassName.php" and use this for autoload.
(BTW that those frameworks like Zend Framework or Doctrine ORM recommend)

As a support: you say that you are not a big fan of autoloaders; What for? I use them as much as I can:

  • usually better for performance: only the code you really use is loaded / parsed /

  • less code to write (no require / include )
+7
source share

I use ClassName.class.php for class files and SomeDescription.lib.php for files without a class.

Not a fan of .inc.php . It seems incorrect to describe the file in terms of how it can be imported, not its contents.

+6
source share

All Articles