Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Fatal error: Cannot redeclare function [duplicate]

Tags:

php

apache

I have a function A in file B.inc

line 2:  function A() {

           ...

line 10: }

In the apache log:

PHP Fatal error: Cannot redeclare A() (previously declared in B.inc:2) in B on line 10

like image 247
Bruce Dou Avatar asked Mar 17 '11 01:03

Bruce Dou


3 Answers

I suppose you're using require "B.inc" in multiple parts? Can you try using require_once in all those instances instead?

Seems like your B.inc is parsed twice.

like image 183
EboMike Avatar answered Nov 20 '22 12:11

EboMike


I had a similar problem where a function entirely contained within a public function within a class was being reported as redeclared. I reduced the problem to

class B {
  function __construct() {
   function A() {
   }
  }
 }
 $b1 = new B();
 $b2 = new B();

The Fatal error: Cannot redeclare A() is produced when attempting to create $b2.

The original author of the code had protected the class declaration from being redeclared with if ( !class_exists( 'B' ) ) but this does not protect the internal function A() from being redeclared if we attempt to create more than one instance of the class.

Note: This is probably not the same problem as above BUT it's very similar to some of the answers in PHP Fatal error: Cannot redeclare class

like image 37
bobbingwide Avatar answered Nov 20 '22 10:11

bobbingwide


Did you already declare A() somewhere else?

Or, are you calling B.inc twice on accident?

try using: require_once("B.inc");

like image 3
Shackrock Avatar answered Nov 20 '22 12:11

Shackrock