[PHP] PHP Fatal error: Cannot redeclare class

2011-03-14 Thread Brendan_Crowley
Hi, I get the following error in my apache error log:
[14-Mar-2011 14:17:27] PHP Fatal error:  Cannot redeclare class
I created a simplified set of php files and classes to formulate this question.
I have 5 php files; 2 of which declare a class that has the same name in both. 
I know it would be very easy to change one of the class names, but I'm 
restricted because I'm working with legacy working code and an interface 
definition that cannot change either, here are the php files, how can I resolve 
this?, any help appreciated.
Thanks,
Brendan.

## phpFileOne.php ##
?php
include_once './phpFileTwo.php';
include_once './phpFileFour.php';

$zIns = new Z();
$result = a($zIns);

// class Z is defined in phpFileFour.php
function a(Z $dataX) {
  $dataZ;
  $dataY = b($dataZ); // b is defined in phpFileTwo.php
  return $dataY;
}

echo 'presult: '.$result.'/p';
?

## phpFileTwo.php ##
?php
include_once './phpFileThree.php';
function b($dataW) {
  $dataU;
  $dataV = new A($dataU); // class A is defined in phpFileThree.php
  return $dataV-c();
}
?

## phpFileThree.php ##
?php
include_once './phpFileFive.php';
// class Z is defined in phpFileFive.php
class A extends Z {
  function __construct($dataM) {

  }

  function c() {
return 'cValue';
  }
}
?

## phpFileFour.php ##
?php
// class Z defined in interface definition - cannot change name
class Z {
}
?

## phpFileFive.php ##
?php
// this class and class Z in phpFileFour.php have the same name!
// Legacy class Z - cannot change name
class Z {
}
?







Re: [PHP] PHP Fatal error: Cannot redeclare class

2011-03-14 Thread Daniel Brown
On Mon, Mar 14, 2011 at 10:34,  brendan_crow...@dellteam.com wrote:
 Hi, I get the following error in my apache error log:
 [14-Mar-2011 14:17:27] PHP Fatal error:  Cannot redeclare class
 I created a simplified set of php files and classes to formulate this 
 question.
 I have 5 php files; 2 of which declare a class that has the same name in 
 both. I know it would be very easy to change one of the class names, but I'm 
 restricted because I'm working with legacy working code and an interface 
 definition that cannot change either, here are the php files, how can I 
 resolve this?, any help appreciated.

If it's legacy code and this is how it was, that means it never
worked properly, because you can't redeclare classes or functions by
the same name.  That said, if you're restricted from changing the
class names, how about wrapping the classes with a !class_exists()
call?

http://php.net/class_exists


 ## phpFileOne.php ##
 ?php
 include_once './phpFileTwo.php';
 include_once './phpFileFour.php';

 $zIns = new Z();
 $result = a($zIns);

 // class Z is defined in phpFileFour.php
 function a(Z $dataX) {
      $dataZ;
      $dataY = b($dataZ); // b is defined in phpFileTwo.php
      return $dataY;
 }

 echo 'presult: '.$result.'/p';
 ?

 ## phpFileTwo.php ##
 ?php
 include_once './phpFileThree.php';
 function b($dataW) {
      $dataU;
      $dataV = new A($dataU); // class A is defined in phpFileThree.php
      return $dataV-c();
 }
 ?

 ## phpFileThree.php ##
 ?php
 include_once './phpFileFive.php';
 // class Z is defined in phpFileFive.php
 class A extends Z {
      function __construct($dataM) {

      }

      function c() {
            return 'cValue';
      }
 }
 ?

 ## phpFileFour.php ##
 ?php
 // class Z defined in interface definition - cannot change name
 class Z {
 }
 ?

 ## phpFileFive.php ##
 ?php
 // this class and class Z in phpFileFour.php have the same name!
 // Legacy class Z - cannot change name
 class Z {
 }
 ?









-- 
/Daniel P. Brown
Network Infrastructure Manager
http://www.php.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] PHP Fatal error: Cannot redeclare class

2011-03-14 Thread Brendan_Crowley
Thanks Louis,

Worked a treat!

-Original Message-
From: Louis Huppenbauer [mailto:louis.huppenba...@gmail.com] 
Sent: 14 March 2011 15:10
To: Crowley, Brendan - Dell Team
Subject: Re: [PHP] PHP Fatal error: Cannot redeclare class

what about http://ch2.php.net/namespaces in php 5.3?

2011/3/14  brendan_crow...@dellteam.com:
 Hi, I get the following error in my apache error log:
 [14-Mar-2011 14:17:27] PHP Fatal error:  Cannot redeclare class I 
 created a simplified set of php files and classes to formulate this question.
 I have 5 php files; 2 of which declare a class that has the same name in 
 both. I know it would be very easy to change one of the class names, but I'm 
 restricted because I'm working with legacy working code and an interface 
 definition that cannot change either, here are the php files, how can I 
 resolve this?, any help appreciated.
 Thanks,
 Brendan.

 ## phpFileOne.php ##
 ?php
 include_once './phpFileTwo.php';
 include_once './phpFileFour.php';

 $zIns = new Z();
 $result = a($zIns);

 // class Z is defined in phpFileFour.php function a(Z $dataX) {
      $dataZ;
      $dataY = b($dataZ); // b is defined in phpFileTwo.php
      return $dataY;
 }

 echo 'presult: '.$result.'/p';
 ?

 ## phpFileTwo.php ##
 ?php
 include_once './phpFileThree.php';
 function b($dataW) {
      $dataU;
      $dataV = new A($dataU); // class A is defined in phpFileThree.php
      return $dataV-c();
 }
 ?

 ## phpFileThree.php ##
 ?php
 include_once './phpFileFive.php';
 // class Z is defined in phpFileFive.php class A extends Z {
      function __construct($dataM) {

      }

      function c() {
            return 'cValue';
      }
 }
 ?

 ## phpFileFour.php ##
 ?php
 // class Z defined in interface definition - cannot change name class 
 Z { } ?

 ## phpFileFive.php ##
 ?php
 // this class and class Z in phpFileFour.php have the same name!
 // Legacy class Z - cannot change name class Z { } ?