functions and methods in PHP are case-insensitive?


Some time ago, due to an accident at the time of a debug I realized that PHP is not case sensitive at the time of a function call.





The same thing happens for the methods of the class:

    $fileIterator = new FileSystemIterator(__DIR__);


    foreach ($fileIterator as $file) {
        echo   $file->getRealPath();
        echo $file->getRealpath();
        echo $file->GETREALPATH();


To find out what the original name of the getRealPath method was, I used get_class_methods in FileSystemIterator . And the result was:

[32] => getRealPath

And in the PHP Manual, too.

The question is: even though case-insensitive , because of having a "default name" defined for methods and functions, should I bother writing them exactly as they are in the manual?

For my memory, I know that FileSystemIterator has a method called getRealPath , but sometimes I forget how to spell (if it is getRealPath or getRealpath ), and, because it works, I leave it the way that's it.

Should I worry about this "writing" at the time of the method call?

asked by anonymous 11.02.2015 / 15:24

2 answers


Are case sensitive :

  • variables
  • constants
  • arrays keys
  • class properties
  • class constants

They are not case sensitive :

  • functions
  • class constructors
  • class methods
  • keywords and language constructs (if, else, null, foreach, echo, etc.)

Does this differentiation make sense? Not for me. Although there may be a technical explanation for this, or everything should be sensitive or nothing should be.

The first are hashes keys, so it is more complicated to give insensitivity - although it is possible (but changing the implementation and not using hash ). The second group is done by the compiler and it is easier to resolve the insensitivity.

In what is not sensible my advice is to write correctly even if it is not necessary. For no specific reason other than to write in a clean, correct way, to practice doing right always. If there are no disadvantages in doing the right thing, then do it.

11.02.2015 / 15:40

Functions, just like class methods, are not case sensitive.


Note: Function names are case-insensitive, although it is usually good form to call functions as they appear in their declaration.

Source: link

I believe this behavior is due to exactly the reason you've exposed (method method names, native functions, and user-defined functions). Usually the names functions are composed of two or more words and there they give rise to these doubts.

Just by complementing: Although the PHP function names do not follow a specific standard (including this is one of the main criticisms of the language), there are several conventions adopted in PHP (and other languages):

  • Class method names are camelCase with the lowercase initial;
  • Native and user-defined function names are snake_case .

Try to remember these rules in addition to the quirks of some PHP functions, and you'll get almost 100% correct in function names and methods.

11.02.2015 / 15:30