According to PHP's documentation class_exists()
is not case sensitive. However, I'm experiencing that it is. For example, class_exists("\\My\\Class")
returns true but class_exists("\\My\\class")
returns false.
I'm running PHP 5.3.3. I have two separate environments. This is working correctly in one environment, but the other environment is acting like it cares about case.
What am I missing? Is there a config setting somewhere?
UPDATE:
For anyone else experiencing this problem, I found the issue. class_exists()
uses the autoloader for any classes that have not been declared. class_exists()
will behave as case insensitive as long as the class shows up in the list returned by get_declared_classes()
. However, if the class you are looking for does not show up in this list, it relies on the registered autoloader stack to find it. The autoloader my project is using is Symfony2's UniversalClassLoader which ultimately relies on file_exists()
to autoload the class. file_exists()
IS case sensitive as long as the system's environment is case sensitive. This is why I was seeing the problem on one environment and not the other. There are many ways to solve this problem by adding an autoloader that is not case sensitive. There are also some good examples of some case insensitive file_exists()
implementations in the documentation comments.
I sincerely apologize for wasting everyone's time by not effectively communicating the problem and providing a pseudo-example instead of actual code. Rather than my intention of getting to the core of my problem, my pseudo-example was a distraction. I have failed and for that I am truly sorry.
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. In the following example, the function that is defined as exampleFunction() is called as ExampleFunction() , which is discouraged.
Summary. PHP is partially case-sensitive. PHP constructs, function names, class names are case-insensitive, whereas variables are case-sensitive.
Tip: The strcasecmp() function is binary-safe and case-insensitive. Tip: This function is similar to the strncasecmp() function, with the difference that you can specify the number of characters from each string to be used in the comparison with strncasecmp().
PHP is a unique programming language in terms of case sensitivity. In PHP, variables and constants are case sensitive, while functions are not case sensitive. PHP classes are a mix between variables and functions, so they are partially case-sensitive.
Are you comparing windows/mac to linux?
Assume the file Wooby\Dooby\Foo.php
exists. With the following contents:
<?php
namespace Wooby\Dooby;
class Foo {}
If a class already exists, it doesn't matter what case you use to refer to it, the class will be found:
<?php
require "Wooby/Dooby/Foo.php";
echo "Class Wooby\\Dooby\\foo does " . (class_exists("Wooby\\Dooby\\foo") ? '' : "NOT") . " exist\n";
echo "Class wooby\\dooby\\foo does " . (class_exists("wooby\\dooby\\foo") ? '' : "NOT") . " exist\n";
echo "Class Wooby\\Dooby\\Foo does " . (class_exists("Wooby\\Dooby\\Foo") ? '' : "NOT") . " exist\n";
Running the above test file would return:
-> php index.php
Class Wooby\Dooby\foo does exist
Class wooby\dooby\foo does exist
Class Wooby\Dooby\Foo does exist
If a class does not exist and you use an autoloader - then case does matter. Consider the above example modified to use a simple autoloader:
<?php
ini_set('display_errors', 0);
function __autoload($name) {
$file = str_replace('\\', '/', $name) '.php';
if (file_exists($file)) {
include $file;
}
}
echo "Class Wooby\\Dooby\\foo does " . (class_exists("Wooby\\Dooby\\foo") ? '' : "NOT") . " exist\n";
echo "Class wooby\\dooby\\foo does " . (class_exists("wooby\\dooby\\foo") ? '' : "NOT") . " exist\n";
echo "Class Wooby\\Dooby\\Foo does " . (class_exists("Wooby\\Dooby\\Foo") ? '' : "NOT") . " exist\n";
The results would be:
-> php index.php
Class Wooby\Dooby\foo does NOT exist
Class wooby\dooby\foo does NOT exist
Class Wooby\Dooby\Foo does exist
Because the autoloader is looking for paths which match the missing classname, only the last entry triggers including a file and loading the class.
Unless you're using windows or a mac1 which both use case-insensitive file systems.
Class names in php are not case-sensitive, but your code likely is as it effectively inherits the case-sensitivity of the file-system. Obviously it's best to use consistent case and not rely on php correcting lazy development habits.
Note that class_exists has a parameter to turn on or off (on by default) the use of an autoloader when looking for none-existent classes.
1 More accurately HFS is, by default, case-insensitive but case-preserving.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With