tags:

views:

687

answers:

5

I get this fatal error only when I run on a Macintosh, but not on a windows browser, which doesn't make sense since, other than checking for the Browser condition, the conditional loops run the same code:

Can someone help me understand how to stop this error in php? The error occurs on the FIRST instance of QEnterKeyEvent...and NOT the second. This doesn't make sense.

In the code, the first instance is the first time it is ever called, so the class has not yet been created as far as I can tell.

Yet the error says: cannot redeclare class QEnterKeyEvent

// Key-Specific Events (EnterKey, EscapeKey, UpArrowKey, DownArrowKey, etc.)

if (QApplication::IsBrowser(QBrowserType::Macintosh)) {
 echo "keyspecific events - macintosh";

 class QEnterKeyEvent extends QKeyPressEvent {
  protected $strCondition = 'event.keyCode == 13';
 }
 class QEscapeKeyEvent extends QKeyPressEvent {
  protected $strCondition = 'event.keyCode == 27';
 }
 class QUpArrowKeyEvent extends QKeyPressEvent {
  protected $strCondition = 'event.keyCode == 38';
 }
 class QDownArrowKeyEvent extends QKeyPressEvent {
  protected $strCondition = 'event.keyCode == 40';
 }
} else {
 echo "key specific events - windows";

 class QEnterKeyEvent extends QKeyDownEvent {
  protected $strCondition = 'event.keyCode == 13';
 }
 class QEscapeKeyEvent extends QKeyDownEvent {
  protected $strCondition = 'event.keyCode == 27';
 }
 class QUpArrowKeyEvent extends QKeyDownEvent {
  protected $strCondition = 'event.keyCode == 38';
 }
 class QDownArrowKeyEvent extends QKeyDownEvent {
  protected $strCondition = 'event.keyCode == 40';
 }
}
A: 

Both blocks of code define the same class:

class QEnterKeyEvent extends QKeyDownEvent {
            protected $strCondition = 'event.keyCode == 13';
}

From the sample code you give, it makes more sense to define that class outside of the if / else statements entirely, which will both reduce the amount of code to maintain and resolve your error.

However, I suspect you may be mismatching the braces when reading the code; from the snippet as you paste it, the error shouldn't occur.

If the second class definition is occurring as well as the first (because they aren't part of the same conditional) then you would expect to see that error.

If you decide to not restructure your code to avoid duplication (you may have good reasons for the current structure, ones which aren't indicated in the snippet you give), you could use PHP's class_exists() to avoid an attempt at duplicating the class definition:

} else {
  if ( !class_exists('QEnterKeyEvent') ) {
    class QEnterKeyEvent extends QKeyDownEvent {
      protected $strCondition = 'event.keyCode == 13';
    }
  }
}

If you do use this approach, it would make sense to check carefully that the class is defined as you'd expect it in all cases.

PS. Is this code sample from the QCodo codebase? If so, you probably ought to report the issue via their "Bugs and Issues" forum.

PPS. Oh look. See this and this ...

Chris Burgess
Chris, thanks....the reason the two blocks of code define the same class is because one is in the instance where the Browser is Macintosh. The error appears to happen on the *first* instance of the code...very strange...maybe I will put in the condition to check if it exists....
AFG
+1  A: 

I see this code sample is from the QCodo codebase? In that case you probably ought to report the issue via their "Bugs and Issues" forum. After a look there, I see there are already a couple of reports: this and this ...

Here's the full code block (from a forum post on the Qcodo site):

// Key-Specific Events (EnterKey, EscapeKey, UpArrowKey, DownArrowKey, etc.)

if (QApplication::IsBrowser(QBrowserType::Macintosh)) {
    class QEnterKeyEvent extends QKeyPressEvent {
        protected $strCondition = 'event.keyCode == 13';
    }
    class QEscapeKeyEvent extends QKeyPressEvent {
        protected $strCondition = 'event.keyCode == 27';
    }
    class QUpArrowKeyEvent extends QKeyPressEvent {
        protected $strCondition = 'event.keyCode == 38';
    }
    class QDownArrowKeyEvent extends QKeyPressEvent {
        protected $strCondition = 'event.keyCode == 40';
    }
} else {
    class QEnterKeyEvent extends QKeyDownEvent {
        protected $strCondition = 'event.keyCode == 13';
    }
    class QEscapeKeyEvent extends QKeyDownEvent {
        protected $strCondition = 'event.keyCode == 27';
    }
    class QUpArrowKeyEvent extends QKeyDownEvent {
        protected $strCondition = 'event.keyCode == 38';
    }
    class QDownArrowKeyEvent extends QKeyDownEvent {
        protected $strCondition = 'event.keyCode == 40';
    }
}

So on a second read, I believe the issue is not that this block of code hits both class definitions, but that this block of code actually is getting called twice. And it looks (from a glance at Qcodo) like this would occur if Qcodo is initialised twice.

In QApplicationBase::Initialize() there is the following code:

  // Preload Class Files
  foreach (QApplication::$PreloadedClassFile as $strClassFile)
      require($strClassFile);

Try replacing that with the code below and see if the issue is resolved? If so, you may have inadvertently initialised Qcodo twice somewhere in your code.

  // Preload Class Files
  foreach (QApplication::$PreloadedClassFile as $strClassFile)
      require_once($strClassFile);
Chris Burgess
ah, me thinks you may have solved it, genius...let me check it out!
AFG
if so, do chase Qcodo to fix it. seems like the problem is not just yours.
Chris Burgess
A: 

If it gets called twice it would do that for every system...

A: 

I've run into this same issue while attempting to install XSilva's Lightspeed Webstore for a client. After some digging and testing, I think I've solved the issue. (FYI simply changing 'require' to 'require_once' -- though a good idea -- didn't work for me.) I know it's an ugly hack, but I wrapped the problematic code block with a class_exists() check as follows:

// Key-Specific Events (EnterKey, EscapeKey, UpArrowKey, DownArrowKey, etc.)
if (QApplication::IsBrowser(QBrowserType::Macintosh)) {
    if(!class_exists('QEnterKeyEvent') and !class_exists('QEscapeKeyEvent') and !class_exists('QUpArrowKeyEvent') and !class_exists('QDownArrowKeyEvent')) {
        class QEnterKeyEvent extends QKeyPressEvent {
            protected $strCondition = 'event.keyCode == 13';
        }   
        class QEscapeKeyEvent extends QKeyPressEvent {
            protected $strCondition = 'event.keyCode == 27';
        }   
        class QUpArrowKeyEvent extends QKeyPressEvent {
            protected $strCondition = 'event.keyCode == 38';
        }   
        class QDownArrowKeyEvent extends QKeyPressEvent {
            protected $strCondition = 'event.keyCode == 40';
        }   
    }   
} else {
    class QEnterKeyEvent extends QKeyDownEvent {
        protected $strCondition = 'event.keyCode == 13';
    }   
    class QEscapeKeyEvent extends QKeyDownEvent {
        protected $strCondition = 'event.keyCode == 27';
    }   
    class QUpArrowKeyEvent extends QKeyDownEvent {
        protected $strCondition = 'event.keyCode == 38';
    }   
    class QDownArrowKeyEvent extends QKeyDownEvent {
        protected $strCondition = 'event.keyCode == 40';
    }   
}   
matt
A: 

There's a community branch of QCodo called QCubed, and they appear to have a fix in QA right now. http://trac.qcu.be/projects/qcubed/ticket/134

VexedPanda