3

Is it common practice to append use '.class.php' extension for PHP classes?

On PHP.net here: http://php.net/manual/en/function.spl-autoload-register.php there are some examples like this:

// Or, using an anonymous function as of PHP 5.3.0
spl_autoload_register(function ($class) {
    include 'classes/' . $class . '.class.php';
});

which use a .class.php extension.

Should PHP code be written like this? I've never seen it before, is it something new? This is a kind of new feature in PHP so maybe it is?

EDIT: 'feature' was not a good word! Perhaps I should have asked whether it's some standard or convention.

Thanks.

3
  • 1
    This is a convention, not a feature. You can call your files whatever you'd like. It would be perfectly valid, for example, to add a .blah.whatever extension, or no extension at all. It's advisable to at least use .php as this helps insure your code is evaluated server-side instead of being served up as plain-text, should the user navigate to the URL of the file directly. Commented Oct 31, 2012 at 14:53
  • Isn't most php-files classes nowadays, except some controller-configs. (It kind of answers the question.) Commented Oct 31, 2012 at 14:58
  • @user247245: no, not "most php". only if the project is done using OOP. Commented Oct 31, 2012 at 15:11

7 Answers 7

8

It's not a feature, it's just a convention that you see come up every now and then. Whether to follow it or not is the choice of you and your team.

My personal opinion is that since the choice is arbitrary and this particular style contradicts with the PSR-0 autoloader specification you should pass because:

  • PSR-0 is more widely used, so all other things being equal it would be a better convention to follow
  • following the ".class.php" style means cannot take a PSR-0 compliant autoloader (there are many online) and use it without modifications
Sign up to request clarification or add additional context in comments.

4 Comments

The question doesn't imply this is a feature - when he says "this is a new feature" he's referring to classes in general.
@PWhite: PHP has had classes for longer than I can remember -- roughly 10 years would be about right -- so I 'm not sure how they would be a new feature.
Had to check that. Didn't realize PHP5 was 2004. How time flies.
@PWhite: Not to mention that they were in PHP4 as well. ;-)
1

This is mostly used to assure its a class file the autoloader targets, you often have other .php files like templates, scripts like bootstrapping or config files that are .php files, but should never be interpreted as classes

Comments

1

It's not a feature as such, it's just a very simple, and practical way to keep your code organized and, more importantly, to avoid issues with autoloading.

Suppose you've got some class called User, and your site has a page, that is generated by a script: User.php. If you need an instance of the User class, the autoloader function will be called, and get User as an argument. When looking for a file simply called User.php, you might include a file, other than the class definition. That's why you can (and should) give class definition a little extra in their names. Then you can write your autoloader to look for [[class name]].class.php, neatly avoiding the User.php file.

That's the bottom line of it. There's -of course- also namespaces to consider, and that most modern way of all to keep your code organized: directories (set_include_path)

Comments

0

It is all for better understanding what this files all about. So, when you see filename.class.php, you know, this file contains Class filename.

Comments

0

There isn't any kind of special language support that gets triggered if you name your classes 'class.php'; it's just a convention, like whether you use spaces or tabs for indentation.

In one of the projects I worked on, classes where named *.inc.php, and templates *.tpl.php.

Comments

0

If I was coding php circa 1999, this would be an excellent convention to follow. You can name files whatever you want, but I recommend just naming them after the classname inside the file, with '.php' appended.

Comments

0

This is more of a personal preferences, but I would actually probably look into the PSR-0 standards which provide a standard way for you to name and namespace your classes to provide better compatibility with other peoples code in the same format.

In the PSR-0 standards PHP files are named after the class and put into folders related to the namespace.

I would recommend adopting this method over the .class.php method mentioned in the PHP.net manual.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.