Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should I name my PHP class file? [closed]

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?

like image 977
johnnietheblack Avatar asked Jul 30 '09 22:07

johnnietheblack


People also ask

How do I name a PHP class file?

should be in all lowercase. Furthermore, class file names should match the name of the class itself. For example, if you have a class named Myclass , then its filename must be Myclass. php.

Should class names be capitalized PHP?

PHP is case insensitive for the class naming. it means you can normally do $file = new file() even if the class is named File and vice-versa.

Should class file names be capitalized?

The official convention is to use all lower case for file names (as others have already stated).

What is class file in PHP?

Key Aspects of a PHP ClassDefine a class with keyword “class” followed by name of the class. Define the constructor method using “__construct” followed by arguments. The object of the class can then be instantiated using “new ClassName( arguments_list )” Define class variables.


2 Answers

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.


Update: With PHP coding standard PSR-4, there is now a semi-official way to name your class files. The previous advice—to name your class file the same as the class with the .php extension—still stands. But now you are expected to place that class file in a subdirectory named after your class namespace; PSR-4 requires that all of your classes be contained in a namespace defined by you.

What do you get for this? Autoloading for free! If you’re using Composer, you can specify the top-level directory for your classes and Composer will autoload them. No more require statements to load your classes.

If you don’t want to do this, you don’t have to: PSR-4 is a recommendation, not a requirement.

like image 188
Nate Avatar answered Oct 12 '22 04:10

Nate


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. (I used to use ClassName.class.php.)

like image 22
chaos Avatar answered Oct 12 '22 04:10

chaos