Re: PHP Fatal error: Cannot redeclare class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Mar 14, 2011 at 10:34,  <Brendan_Crowley@xxxxxxxxxxxx> 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 '<p>result: '.$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




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux