Reputation: 9415
Here is my class script:
class.php
<?php
class testing {
function hurray () {
echo "Works.";
}
}
And my calling script is:
test.php
<?php
include_once "class.php";
include_once "claSs.php";
$a = new testing();
$a->hurray();
When I try to execute test.php
php test.php
I get the below error:
PHP Fatal error: Cannot redeclare class testing in /private/var/www/temp/ClaSs.php on line 2
The *Nix OS supports case-insensitive file-system meaning it understands the case used to refer the filenames. Having said that the above script is loading the same file with the change in the case and Mac or PHP thinks it's a different file and loading it in the runtime which results in the above error.
My Question:
Additional Information:
My Mac OS X has been built with Mac OS Extended (Journaled) as per this link:
File System Personality: Journaled HFS+
Type (Bundle): hfs
Name (User Visible): Mac OS Extended (Journaled)
Upvotes: 2
Views: 526
Reputation: 9415
To answer my own question:
Does Mac OS X think it's two different files?
Yes. Mac is configured with case-insensitivity but it respects case, so if you have two includes for the same file but have mixed case it will give you the file respecting the case as if they were different files, this causes PHP to load both files and the second include will be a class collision.
Does PHP think it's two different files? I doubt because I tried the above test on Windows and it worked like a charm.
Please read answer for #1.
Is my understanding wrong?
My understanding is correct but it preserves the case.
What is the best way to solve this problem because I don't end up changing 100s of legacy files.
To avoid such errors, you need to ensure that a same file is not loaded with different case.
Upvotes: 0
Reputation: 8613
You could have a look at here where there is already a discussion about that topic.
From my experience I would answer:
Upvotes: 1
Reputation: 1675
Mac OS does indeed treat the two filenames as different.
<?php
include_once "file.php";
// Prints current file and file.php
print_r(get_included_files());
include_once "file.php";
// Didn't add it more than once
print_r(get_included_files());
include_once "FILE.php";
// Prints current file, file.php and FILE.php
print_r(get_included_files());
Upvotes: 1