Jump to content

"Cannot redeclare class" which uses extends


pendelton

Recommended Posts

This problem has been driving me mad for the past 13 hours. I have php 4 on an existing server and php 5.1.2 on a new server (which I am trying to migrate to). The below test code works fine on php 4, but fails with "Fatal error: Cannot redeclare class..." with php 5. The error only occurs for classes that use extends. Is there a bug in php 5? Has it been fixed in a later version (not that my SUSE server will let me install a later version despite trying for hours). Can someone please help or at least test this out for my on other versions of PHP?

You will need 3 simple files to reproduce this problem. I know that the double include in test.php doesn’t make immediate sense but it simulates multiple includes from different file in a php project that contains over 2,000 files (remember, this all works fine on php 4). If you take out the "extends CTest3" from test2.inc and it works fine on PHP5.

File 1: test.php
<?
require "test2.inc";
require "test2.inc";
?>

File 2: test2.inc
<?

if (defined("CTEST2")) return;
define("CTEST2", 1);

require "test3.inc";

class CTest2 extends CTest3
{

function CTest2()
{
print "Test2";
}

}

?>

File 3: “test3.inc”
<?

if (defined("CTEST3")) return;
define("CTEST3", 1);


class CTest3
{

function CTest3()
{
print "Test3";
}

}

?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.