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:
PHP Fatal error: Cannot redeclare class.
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.]
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.