Jump to content

Archived

This topic is now archived and is closed to further replies.

pendelton

"Cannot redeclare class" which uses extends

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";
}

}

?>

Share this post


Link to post
Share on other sites
Instead of using the define() and return, just use require_once (or include_once).

Share this post


Link to post
Share on other sites

×

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.