We help IT Professionals succeed at work.

I received a Fatal Error in PHP5-  Fatal error: Cannot redeclare class invalidargumentexception  What does this mean? How do I fix it?

jerrylmclaughlin
on
Medium Priority
1,188 Views
Last Modified: 2012-05-05

The code is below. What does this mean?

class InvalidArgumentException extends Exception
{
  function __construct($in_argname)
  {
    parent::__construct("We're sorry, but an internal programming error has occurred in the web application.  The system administrators have been notified of the error and we kindly request that you try again in a little while. (param: $in_argname)");
  }
}
Comment
Watch Question

CERTIFIED EXPERT

Commented:
It sounds like your class has already been defined and it's trying to define it a second time.  Make sure that the file which contains this class is not accidentally getting included twice in your script.
I agree with Tomeeboy.
If that is the case you can use the function: include_once()
That will help stop including a script multiple times.

Joe P

Author

Commented:
Thank you for responding. This is code is part of a larger include file. The include file is placed in a file with require_once() function, is this the same.

How do I use the include_once() function with above code?
IT/Programming
CERTIFIED EXPERT
Commented:
You can simply place a test around the declaration to absolutely ensure it is only declared once.

if (!defined('CLASS_INVALIDARG_SET')) {
  define('CLASS_INVALIDARG_SET', 'yup');

  class InvalidArgumentException extends Exception
  {
    function __construct($in_argname)
    {
      parent::__construct("We're sorry, but an internal programming error has occurred in the web application.  The system administrators have been notified of the error and we kindly request that you try again in a little while. (param: $in_argname)");
    }
  }


I use unique include file names for each file I include in the app.  If I screw up and include a file more than once the structures and functions/classes only get delared once.  This is lazy programming, but it works.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks Rob - I tried it just as you placed it above and I got the same error message. I 'm going to try your other suggestion now.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.