Jump to content


"Cannot redeclare class" which uses extends

  • Please log in to reply
1 reply to this topic

#1 pendelton

  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationUK

Posted 28 October 2006 - 10:44 PM

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



#2 toplay

  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 29 October 2006 - 03:44 AM

Instead of using the define() and return, just use require_once (or include_once).

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users