Why Are Functions and Methods in PHP Case-Insensitive

Are PHP functions case sensitive?

I am quoting from this:

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

So, its looks like user-defined functions are not case-sensitive, there was a vote for making functions/objects under PHP5 case-sensitive.

Is PHP function names case-sensitive or not?

They are case insensitive, see this:

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

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

PHP & Case Sensitivity

Why is PHP partially case senstive?

I can only speculate that this stems from very early versions, probably PHP/FI 2.0. The manual explicitely states:

Keep in mind that PHP/FI function names are not case sensitive.

Most user input, such as GET and POST parameters, has always been registered as global variables, back then. Treating these as case insensitive would likely have caused issues, and supposedly therefore all variables have been treated as being case sensitive.

From what I can tell these have been the only kinds of identifiers in PHP/FI 2.0. All others have been introduced later, apparently mimicking the case-insensitive function names.

Constants, which are special, have only been introduced as of PHP 4 (the PHP 3 manual mentions "constants", but these are nowadays referred to as "literals"). For some mysterious reason (maybe no consensus could be found), it had been decided to allow constant identifiers to be define()d either case sensitive or insensitive on the developers discression. Interestingly, while define() defaults to case sensitive constants, the respective C counterparts (REGISTER_*_CONSTANT) default to case insensitive.

Are PHP functions case-sensitive, if not...?

This hints at suffering from "the I problem", which manifests itself when PHP is using a Turkish locale (tr_TR, tr_TR.utf8…). When doing so, the case-insensitive check between uppercase and lowercase letter "i" fails.

See https://bugs.php.net/18556 — "Setting locale to 'tr_TR' lowercases class names"


You have a couple of solutions:

  • Define and call your function with same-cased letters (or, at the very least the letter "i"); upper or lower is not important.
  • Use a locale not affected by this (mis)behaviour.

The latter is preferred, mostly because it's usually a one-tiny-change-fixes-all-problems sort of task.

Why is it that PHP constants declared as case insensitive can be reassigned?

Because your first constant (which you saved as case-insensitive) is saved in lowercase as you can read it in the manual:

Note:
Case-insensitive constants are stored as lower-case.

Means since it is case-insensitive all variants of lower and upper case from test, which are != TEST in uppercase are corresponding to the value 10. If it is TEST which is case-sensitive means every letter in uppercase it is the constant with the value 20.

E.g.

Test -> 10
tEst -> 10
tesT -> 10
TEST -> 20

And a "special case" is also TEST if you use it before you define your case-sensitive constant it is still pointing to the constant with the value 10.

PHP: Dealing with function case-insensitivity

It's a bit messy, and I wouldn't normally recommend it, but you can simulate case-sensitive methods with PHP's "magic" __call method. This gets called whenever a method can't be found in the given class. You can then check the supplied method name, and run whatever logic is appropriate.

class Foo
{
public function __call($name, $args)
{
switch ($name) {
case 'Execute':
return $this->oldExecute(...$args);
case 'execute':
return $this->newExecute(...$args);
}
}

private function oldExecute($x)
{
echo 'Running old function with arg: ', $x, PHP_EOL;
}

private function newExecute($x, $y, $z)
{
echo 'Running new function with args: ', implode(',', [$x, $y, $z]), PHP_EOL;
}
}

See https://eval.in/926262

Php: are magic functions like magic constants: case insensitive?

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

http://www.php.net/manual/functions.user-defined.php

This has to do how function names are stored in PHP internally. And for some languages (like Turkish) this can even lead to problems:

  • Bug #18556 - Setting locale to 'tr_TR' lowercases class names

Variable names on the other hand are case-sensitive:

The variable name is case-sensitive.

http://php.net/manual/language.variables.basics.php



Related Topics



Leave a reply



Submit