Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capital letters in class name PHP

Tags:

I have two classes in my system. One is called file and second is File. On my localhost when i instantiate file i get file object, but my friend running the same script gets object of File like the capital letters were unrecognized and "file" was equal to "File". Is that some configurable thing? We are both running on Windows. I have WampServer, he has XAMPP.

like image 515
Somal Avatar asked Mar 10 '11 13:03

Somal


People also ask

Should class names be capitalized PHP?

Class names should be descriptive nouns in PascalCase and as short as possible. Each word in the class name should start with a capital letter, without underscore delimiters. The class name should be prefixed with the name of the “parent set” (e.g. the name of the extension) if no namespaces are used.

Is PHP class name case sensitive?

In PHP, class names as well as function/method names are case-insensitive, but it is considered good practice to them functions as they appear in their declaration.

How to name PHP classes?

Class names are always written in UpperCamelCase . The unqualified class name must be meant literally even without the namespace. Class names must be nouns, never adjectives. The name of abstract classes must start with the word “Abstract”, class names of aspects must end with the word “Aspect”.

Is constructor capitalized?

The name of the constructor is the name of the class, so capitalize the constructor name if and only if you capitalized the class name. (If you don't, then the compiler won't even recognize it as a constructor anyway.) For other languages, use the same naming convention you use for the rest of the class's methods.


1 Answers

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.

Are you by any chance relying on the auto loading of class files ? If this is the case, it is possible that depending on the computer, the interpreter don't always find the same file first. This will explain the problem.

I strongly suggest that you rename your classes. It's always a bad idea to rely on case to differentiate two different things and by convention, class names always start with a capital letter.

If you can't change the class names, I suggest to have a look at php namespaces.

like image 77
krtek Avatar answered Oct 08 '22 18:10

krtek