Are functions and methods in PHP case-insensitive?

Asked

Viewed 2,045 times

10

Some time ago, by an accident at the time of a debug I realized that PHP makes no difference between upper and lower case when calling a function.

Example:

print_r($teste);

print_R($teste);

Print_R($teste);

Also the same thing occurs for class methods:

    $fileIterator = new FileSystemIterator(__DIR__);

    $fileIterator->current();
    $fileIterator->Current();

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

   }

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

[32] => getRealPath

And in the PHP Handbook it is also like this.

The question is, even if it is case-insensitive, on account of having a "default name" set to the methods and functions, I must worry about writing them exactly as they are in the manual?

Well, from my memory, I know FileSystemIterator has a method called getRealPath, but sometimes I forget how to write (if it is getRealPath or getRealpath), and by working, I leave it the way it really is.

I should be concerned with this "writing" at the time of the method call?

2 answers

15


Sane case sensitive:

  • variables
  • constant
  • keys to arrays
  • class properties
  • class constants

Are not case sensitive:

  • function
  • class builders
  • class methods
  • keywords and language constructions (if, Else, null, foreach, echo, etc.)

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

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

In what is not sensitive my advice is to write correctly even if it is not necessary. For no specific reason other than writing in a clean, correct way, practice doing the right thing all the time. If there’s no downside to doing the right thing, then do it.

  • 1

    Case-insensitive and case-sensitive sections are inverted, not?

  • Yeah, I reversed it, I’ll fix it :)

  • 1

    I thought about wanting to write in the right way for another reason: "PHP will change everything in future versions!"

  • It is a concern, but I do not think it valid. If the language does this becomes another language.

  • kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

  • It is not worth mentioning that TRUE and FALSE are case-insensitive, despite being constant (are constructs of language, yes; as you noted, the distinction makes no sense...)?

Show 1 more comment

5

Functions, as well as class methods, are not case sensitive.

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

Source: http://php.net/manual/en/functions.user-defined.php

I believe this behavior is due exactly to the reason you exposed (doubts in method names, native functions and user-defined functions). Usually the function names are composed of two or more words and then end up giving rise to these doubts.

Just to complement: although the names of the PHP functions 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 in other languages):

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

Try to remember these rules, in addition to the peculiarities of some PHP functions, and you will hit almost 100% on the names of functions and methods.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.