Rakesh Sankar
Rakesh Sankar

Reputation: 9415

Mac OS X + PHP: Cannot redeclare class testing

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:

  1. Does Mac OS X think it's two different files?
  2. Does PHP think it's two different files? I doubt because I tried the above test on Windows and it worked like a charm.
  3. Is my understanding wrong?
  4. What is the best way to solve this problem because I don't end up changing 100s of legacy files.

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

Answers (3)

Rakesh Sankar
Rakesh Sankar

Reputation: 9415

To answer my own question:

  1. 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.

  2. 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.

  3. Is my understanding wrong?

    My understanding is correct but it preserves the case.

  4. 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

cb0
cb0

Reputation: 8613

You could have a look at here where there is already a discussion about that topic.

From my experience I would answer:

  1. Yes, OS X knows it's 2 different files.
  2. Native php that comes with os x must be configured different than using linux or windows. I also had this problem when movin code to live server and it stopped working because Linux did not find class "Testing" vs "TEsting".
  3. I think you are right. :)

Upvotes: 1

Lesleh
Lesleh

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

Related Questions