php cannot redeclare function
«Fatal error: Cannot redeclare «
I have a function(this is exactly how it appears, from the top of my file):
And for some reason, I keep getting the error:
Fatal error: Cannot redeclare generate_salt() (previously declared in /Applications/MAMP/htdocs/question-air/includes/functions.php:5) in /Applications/MAMP/htdocs/question-air/includes/functions.php on line 13
I cannot figure out why or how such an error could occur. Any ideas?
18 Answers 18
This errors says your function is already defined ; which can mean :
Solution 1
Solution 2
You should include that file (wherein that function exists) only once. So,
instead of : include («functions.php»);
use: include_once(«functions.php»);
Solution 3
If none of above helps, before function declaration, add a check to avoid re-declaration:
You’re probably including the file functions.php more than once.
you can check first if name of your function isn`t exists or not before you write function By
OR you can change name of function to another name
This answer explains why you shouldn’t use function inside function.
This might help somebody.
I had strange behavor when my *.php.bak (which automaticly was created by notepad) was included in compilation. After I removed all *.php.bak from folder this error was gone. Maybe this will be helpful for someone.
Another possible reason for getting that error is that your function has the same name as another PHP built-in function. For example,
where the checkdate function already exists in PHP.
I don’t like function_exists(‘fun_name’) because it relies on the function name being turned into a string, plus, you have to name it twice. Could easily break with refactoring.
Declare your function as a lambda expression (I haven’t seen this solution mentioned):
Then, at re-execution of said PHP code, the function simply overwrites the previous declaration.
require_once is also useful if the file you’re attempting to include is essential.
I had the same problem. And finally it was a double include. One include in a file named X. And another include in a file named Y. Knowing that in file Y I had include (‘X’)
Since the code you’ve provided does not explicitly include anything, either it is being incldued twice, or (if the script is the entry point for the code) there must be a auto-prepend set up in the webserver config / php.ini or alternatively you’ve got a really obscure extension loaded which defines the function.
means you have already created a class with same name.
That second ExampleReDeclare throw the error.
If your having a WordPress theme problem it could be because although you have renamed the theme in your wp_options table you havn’t renamed the stylesheet. I struggled with this.
I want to add my 2 cent experience that might be helpful for many of you.
If you declare a function inside a loop (for, foreach, while), you will face this error message.
I had this pop up recently where a function was being called prior to its definition in the same file, and it didnt have the returned value assigned to a variable. Adding a var for the return value to be assigned to made the error go away.
You have to deactivate the lite version in order to run the PRO version.
This errors says your function is already defined ; which can mean :
«Cannot redeclare function» instead of «Cannot redeclare class»
We have this error:
So what I dont get is that there should be an error of type » PHP Fatal error: Cannot redeclare class » before the fatal » Cannot redeclare function php «.
What is more strange, is that if you read carefully, the problem arises at the end of the declaration of the function. Here’s the code with the lines:
Two strange things:
Has anyone ever had this problem, and where to check/test to solve it? (Please dont comment on how to optimize / how bad the code is, because it’s not mine and I wont modify it)
1 Answer 1
As you can see here, in order for a method to be callable, all zend_function ‘s must be defined. Since a constructor is a function, too, I suspect all the functions in your xx.Class.php file are parsed. Since the methods are defined within their own (class) scope, there can’t be a name conflict (unless there are duplicate methods), before the zend_object is actually registered.
Besides, any decent bit of OO code registers an autoloader function. What would an object be if the parser didn’t know what to make of that function keyword?
There’s a lot more too it than just that, but I figure that must be why PHP notices why you’re attemting to redefine a function before it notices you’re redifining a class.
A couple of useful links, containing a more detailed description of how the zend engine works with functions/methods can be found here
It’s always useful to keep a tab with the actual source at hand, too, so you can actually see the code they’re talking about
PHP Fatal error: Cannot redeclare class
Does anyone know what can cause this problem?
PHP Fatal error: Cannot redeclare class
19 Answers 19
You have a class of the same name declared more than once. Maybe via multiple includes. When including other files you need to use something like
to prevent multiple inclusions. It’s very easy for this to happen, though not always obvious, since you could have a long chain of files being included by one another.
It means you’ve already created a class.
That second Foo would throw the error.
This will happen if we use any of the in built classes in the php library. I used the class name as Directory and I got the same error. If you get error first make sure that the class name you use is not one of the in built classes.
This error might also occur if you define the __construct method more than once.
Sometimes that happens due to some bugs in PHP’s FastCGI.
Try to restart it. At Ubuntu it’s:
I had the same problem while using autoload like follows:
and in other class there was:
The sollution is to keep namespace compatibility, in my example namespace testClassNamespace; in both files.
This error can also occur if you by mistake put a function inside another function.
PHP 5.3 (an I think older versions too) seems to have problem with same name in different cases. So I had this problem when a had the class Login and the interface it implements LogIn. After I renamed LogIn to Log_In the problem got solved.
Just do one thing whenever you include or require filename namely class.login.php. You can include it this way:
This way it never throws an error.
This function will print a stack telling you where it was called from:
Call this function at the top of the file that includes your class.
This should help you find where you class is being included from multiple times in a complex project.
Fatal error: Cannot redeclare class Database
i have fetal error message say :
Fatal error: Cannot redeclare class Database in C:\wamp\www\pets_new\lib\database.php on line 3
and all connect to database class
3 Answers 3
You include 2 files in a single «run». Think of it like this: All the included files are put together by PHP to create one big script. Every include or require fetches a file, and pastes its content in that one big script.
The two files you are including, both require the same file, which declares the Database class. This means that the big script that PHP generates looks like this:
As you can see class Database is declared twice, hence the error.
For now, a quick fix can be replacing the require(‘database.php’); statements with:
Which checks if that particular file hasn’t been included/required before. If it has been included/required before, PHP won’t require it again.
A more definitive and, IMHO, better solution would be to register an autoloader function/class method, and let that code take care of business.
More on how to register an autoloader can be found in the docs. If you go down this route, you’d probably want to take a look at the coding standards concerning class names and namespaces here. If you conform to those standards, you don’t have to write your own autoloader, and can simply use the universal class loader from Symfony2, or any other framework that subscribes to the PHP-FIG standards (like CodeIgnitor, Zend, Cake. you name it)
Fix PHP Fatal error: Cannot redeclare “function name”
This is a short guide on how to deal with the following PHP error:
Fatal error: Cannot redeclare function name (previously declared in /path/to/file.php:3) in /path/to/other-file.php on line 9
The fatal error above will appear if your code contains two or more PHP functions that have the exact same name. Unlike other programming languages such as Java and C++, PHP does not support Method Overloading. Instead, you must use default / optional function parameters.
Where was the first function defined?
If you read the error carefully, you should be able to see what the issue is. In the example above, the function name in question was already previously declared on LINE 3 in file.php. This led to a fatal error when we attempted to create another function with the exact same name on LINE 9 in other-file.php
Take a look at the following example:
If you attempt to run the code snippet above, a fatal error will be thrown and the script will be killed. This is because I created two separate functions with the name test.
OK, so how do I fix this error?
The fix depends on your PHP application and what you are attempting to achieve.
Rename the other function to something else.
If you do not need a function called test, then you could rename the second function to something else.
In the case above, we could do the following:
As you can see, I simply renamed the second test function to myTest.
Check if the function name has already been used.
If you find yourself in a situation where the function name may or may not exist, then you can check to see if the name of the function has already been defined.
An example of this approach being assigned to the problem that we had above:
In the code snippet above, I used the function_exists function to check if test already exists as a function. Because it does exist in the example above, the second function is never created.
Fatal error: Cannot redeclare Classname::function.
Note that PHP class methods / functions must also abide by the same rule:
If you run the code above, it will result in the following error:
Fatal error: Cannot redeclare Test::test() in /path/to/file.php on line 14
This is because a PHP class cannot have two functions with the exact same name. Note that I used public on the first function and protected on the second function because I wanted to demonstrate how the visibility of the method does not matter in this case.
Hopefully, you found this guide to be informative!