Capital letters in class name PHP

Somal picture Somal · Mar 10, 2011 · Viewed 26.3k times · Source

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.

Answer

krtek picture krtek · Mar 10, 2011

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.