php error fatal error cannot redeclare
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)
PHP with APC: Fatal errors: Cannot redeclare class
Since I installed APC for PHP with PECL I get sometimes these errors: Cannot redeclare class xxx
xxx changes from time to time. I could disable APC but APC improves the performance great! Is there a known bug or could I do something else to prevent these errors? I’m using Ubuntu 8.04 LTS with PHP 5.2.4.
Edit/Update (from comments):
I use the Zend Framework Autoloader and these error never occurred before I enabled APC. A few moments ago I get for example that error: Fatal error: require(): Cannot redeclare class zend_db_adapter_abstract in /paths/app/lib/Zend/Db/Select.php on line 27
7 Answers 7
The combination of the following configs fixed it for me:
Without all 3, I’d constantly get the error, but with all three I seem to no longer get the error :)!
I had the same problem with a bunch of PHP libraries as soon as I enabled APC. After a lot of hair pulling I found that setting apc.include_once_override = 0 cleared things up. Still monitoring but haven’t had a the problem re-occur (before that I was able to induce the error by clearing the apc cache).
Hmmm, seems to be a common issue:
What I just noticed from your specific error message is that you wrote zend_db_adapter_abstract in all-lowercase. A problem with the horrid framework naming schemes and autoloaders is that they keep files in mixed case and expect it so. If your code tried to instantiate it this way, the autoloader might not have found it. APC might be more peculiar here, since it overrides include_once internally, maybe with side-effects.
A solution would be to adapt the Zend autoloader, and manually keep a list of loaded classes and (absolute and lowercased) filenames to proofcheck in lieu of include_once.
Otherwise, try excessive xdebug-ing. Without access to your setup, all we can do is guess here.
Well it is a known problem with apc that it mixes up include_once directivse that are called relatively from different locations.
However this is fixed in later versions.
If you can drill down your code to the class that is loaded twice you could do some checking if the class is already loaded with class_defined or some callable stuff.
Fatal error: Cannot redeclare email() previously declared in
I am creating my login page and when I loaded it up I seemed to get this error saying:
Fatal error: Cannot redeclare email() (previously declared in /home/a4625512/public_html/core/functions/general.php:2) in /home/a4625512/public_html/core/functions/general.php on line 4
My host is 000WebHost so I don’t know if it has something to do with them, and I have searched online everywhere for it.
1 Answer 1
So to clarify what I commented:
The initial part of the error you should be looking at is: Fatal error: Cannot redeclare email()
This error is telling you that your function email() has previously been declared somewhere else, if you look at the rest of the error it is telling you exactly where you should start looking:
/public_html/core/functions/general.php on line 4
So your next steps should be to go to these files and find out why you declared the function name previously.
Some solutions:
You could utilise a namespace to differentiate the email function from your others by putting something like: namepace Example; at the top of your file, although just doing this as a one off to fix the issue is a little hacky and I wouldn’t recommend it.
Or you could (and should) look why you declared it previously and decide what the best option forward is from there.
Bunch of stuff you can do really. To summarise the issue is that you have two function with the exact same name so when you attempt to use them PHP doesn’t know which function you want to use and throws back the error.
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!
php fatal error cannot redeclare class – How to fix?
Receiving an error ‘PHP fatal error cannot redeclare class’? Here’s how we fix it.
A few days ago, we came across this error message due to which our customer was not able to continue with coding for a long time.
At Bobcares, we often get requests to fix PHP fatal errors, as a part of our Server Management Services.
Today, we’ll have a deep look into PHP fatal errors and see how our Support Engineers fix them.
Why does PHP fatal error occur?
We’ve seen many of our customers experiencing this error. Usually, this error causes an immediate termination of the script.
Basically, ‘PHP fatal error cannot redeclare class’ indicates that the class name already exists.
Mostly, this error shows up while the user is adding some new PHP codes. But WordPress sites also show the same error due to plugin or theme conflicts.
Let’s see both these instances in detail.
1. Error while coding
PHP fatal error is quite common while coding. Developers often get the error message when trying to declare an already existing class. The error message appears as:
At this point of error, the script terminates.
Recently, one of our customers approached us with this error. So we suggested him to avoid using already existing class in further coding.
But the new class created by our customer was also giving the same error. So, we provided him the below command to fix the problem.
Here XXXX is the new class name.
2. Cannot redeclare class error in WordPress
Sometimes, WordPress sites show fatal error cannot redeclare class.
Usually, PHP based WordPress site shows this error after installation of new plugins or themes. A typical error message in WordPress looks like:
When our customers approach us with this error message, our Support Team checks for the reason for the error.
In most cases, the error will be with the usage of the same class in function.php and plugin files. So, to fix this error we disable the plugin and install the latest version.
[Need help in fixing PHP fatal errors? – We’ll help you.]
Conclusion
In short, PHP fatal error cannot redeclare class occurs due to reuse of an already existing class name, or plugins in WordPress sites. Today, we saw how our Support Engineers fix this error for our customers.
Related posts:
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.