PHP Fatal error: Cannot redeclare class

Discussion in 'Programming' started by swiminsoda, Aug 24, 2011.

  1. #1
    Any one know what to cause this problem??

    PHP Fatal error: Cannot redeclare class
     
    swiminsoda, Aug 24, 2011 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    You include a class, which is either included before, or there is another class with the same name included before.
     
    ssmm987, Aug 25, 2011 IP
  3. Junioreality

    Junioreality Active Member

    Messages:
    158
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    That happens when you declare a class more than once in a page. You can fix it by either wrapping that class with an if statement(like bellow), or you can put it into it's own file and include_once(), instead of include()
    if(class_exists('TestClass') != true)
    {
    //put class TestClass here
    }
     
    Junioreality, Aug 25, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    if(a boolean variable) != true) is known as a pleonasm and it's very poor (and noob) programming. Use if(!class_exists('TestClass'))
     
    Rukbat, Aug 25, 2011 IP