-1

I have a function called "logToFile" and I'm trying to call it but PHP is thinking that I'm trying to redeclare it.

logToFile:

function logToFile($msg) {
$filename = "log.txt";
$fd = fopen($filename, "a");
$str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
fwrite($fd, $str . "\n");
fclose($fd);

My call to the function:

logToFile("$user->username kicked $arg1 for $arg2.");

Help please?

  • 3
    Do you have a closing bracket as well? Is the function not secretly inside a class? Is it declared in the same file or is that file included? – CompuChip Nov 03 '13 at 17:28
  • Maybe you included the file where your function is in more than once? – putvande Nov 03 '13 at 17:28
  • if PHP says that you're redeclaring it, then you do! Try changing the function name and see if it works. – Ilia Ross Nov 03 '13 at 17:28
  • Does the error message have a line number? Is it really the line where you call the function? – Barmar Nov 03 '13 at 17:30
  • @Barmar: `Is it really the line where you call the function?` The line number in error message will be the line where the function was first *redefined*, no? ([demo](http://3v4l.org/CWjeC)) – Amal Murali Nov 03 '13 at 17:43
  • @AmalMurali The error message contains both. The original line is in `previously declared in /pathname:3`, the redefinition is in `on line 4`. – Barmar Nov 03 '13 at 17:46
  • @Barmar: Sorry, that's what I meant. Please see the updated comment. – Amal Murali Nov 03 '13 at 17:48
  • @AmalMurali That was my point. The question says that when he tries to call the function, it's reporting it as a redefinition. I was suggesting that he check the line number in the error message to see which line is really causing the error, because it's probably _not_ the line where he tries to call the function. – Barmar Nov 03 '13 at 17:51

1 Answers1

5

Well, PHP is not dumb. If PHP says you're re-declaring a function, well, then you are. When the function definition occurs multiple times, PHP will throw a Fatal error, similar to the one below:

Fatal error: Cannot redeclare logToFile() (previously declared in /path/to/script:X) in /path/to/script on line Y

Here, X is the line where you originally declared the function, and Y is where you tried to re-declare (not call, as you state in the question) the function. Check your code to find this line, and remove it.

And to avoid errors like this, you can first check whether a function was defined using function_exists() and then try to declare it:

if (!function_exists('logToFile')) {
    function logToFile($msg) {
        $filename = "log.txt";
        $fd = fopen($filename, "a");
        $str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
        fwrite($fd, $str . "\n");
        fclose($fd);
    }
} else {
    echo 'Trying to re-declare the function';
}

While the above method will help you avoid fatal errors, I strongly suggest you figure out where you're redefining the function and correct that instead. Most of the times, this will be due to multiple includes of the file containing your function. In that case, you can simply use require_once() instead. PHP will check if the file has already been included, and if so, not include (require) it again.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • 3
    Just exactly what I was going to write!! :D – Ilia Ross Nov 03 '13 at 17:28
  • While this will stop the error, it seems like the wrong way to go about it. Figure out why you're trying to redefine the function in the first place, and fix it. – Barmar Nov 03 '13 at 17:48
  • Really not a fan of your `function_exists` approach. It has no benefits over just having PHP display an error and bloats code enormously. – Duncan Jones Jan 02 '14 at 16:34
  • @Duncan: I agree -- which is why I've included the additional note at the end of my answer. – Amal Murali Jan 02 '14 at 16:41