PHP

How to avoid fatal error Cannot redeclare function in PHP

7 Mar , 2010  

This is for the programmers who are using functions to drive a website in PHP or there are some instances when you are using a common file which has some utility functions in it. In this kind of scenario the same file loops around on different pages and there exists this “Fatal error: Cannot redeclare function”. This error will be encountered if you define a function in the following fashion.

<?php
 
function utility()
{
  //your function definition goes here
  return true;
}
?>

Not many of you might face this problem since we use classes and functions are sandwiched inside them with some scope. But to avoid this kind of error you can define the same function in the following fashion,with a check before defining it.

<?php
if(!function_exists('utility'))
{
  function utility()
  {
    //your function definition goes here
    return true;
  }
}
?>

As you see we just check if the function already exists with the help of a PHP’s inbuilt function function_exists(). Hence by using the above methodology you can avoid this fatal error and your code won’t break.

Hope that helps.

Stay Digified !!
Sachin Khosla

,


14 Responses to “How to avoid fatal error Cannot redeclare function in PHP”

  1. Fly says:

    Hi there!
    Thank you very much!
    I have got this problem for along time and I have not any trust guide by google before reading yours.
    Thanks again!

  2. charles says:

    Thankx very much for this function)))i were about to break my head wondering how to solve the problem “Fatal error: Cannot redeclare function”.

    I real appreciate MAN.
    stay blessed.

  3. Paul says:

    HA!! You just saved me hours worth of debugging!!! I got this error when I switched web servers, googled it. This came up. Implemented it. And BAM… fixed!

    THANK YOU!

  4. Thank you very much. I ran into this “Fatal Error: Cannot redeclare function” issue this afternoon and I knew there must be an easy way to get around it.

    Saved me a good few hours of debugging & re-factoring.

  5. Peter Michael Gits says:

    there is another possibility, if you are using include_once and you accidentally type
    include_once(“RoomUtils.php”) in one file and type in
    include_once(“roomUtils.php”) in another, it will have similar behavior and in this case if you are performing unit tests in each file, the one that is misspelled will only work with the unit test.

  6. Ender says:

    I’ve just gotta tell you:

    YOU ARE THE MAN!!!

    This has helped me configure a block on my site that has been bothering me for ages!

    Much kudos to you

  7. Glad to know that it helped :)

  8. Widi Priono [ Jakarta, Indonesia ] says:

    Thanks..it works…..
    glad to know you

  9. Rodolfo says:

    this function is lovely… thanks!

  10. Vishal says:

    Thanks Sachin for this post, it helped!

  11. Dragan says:

    Ah!! Tank you!! was braking my head . Just like you sadi Im using same files over and over and if they are on the same page i get name clash , so since a long time I would rename the function each time I had to reuse the code. THANK YOU!

  12. Edward (drakon) says:

    Thanks so much! this problem has been frustrating me for quite awhile!

  13. irina says:

    Thank you so much, this really saved me. You are great!

More in PHP (18 of 43 articles)


Hey People, Always wondered what could be the API which lets you interact with Google Talk ? Well I guess ...