Fatalerror: Cannotredeclareclass cause analysis and solution. I used the php _ autoload status to automatically load classes. Today, a good program does not know how to prompt Fatalerror: Cannotredeclareclass at runtime. the class is already defined, I am using php _ autoload to automatically load classes. Today, a good program does not know how to prompt Fatal error: Cannot redeclare class at runtime. it seems that the class is already defined, next I will analyze the solution.
Error message
Fatal error: Cannot redeclare class ....
It is quite understandable from the literal point of view. it means that the class has been repeatedly defined, and you have found your own code because of the existence of a class with the same name. you just need to modify the class name.
Cause analysis
1. repeat the same file to declare two classes with the same name:
For example:
The code is as follows: |
|
Class Foo {} // Some code here Class Foo {} ?> |
In the second Foo, an error is reported.
Solution: remove the second Foo or rename it.
To prevent repeated definitions, you can determine whether the class already exists when defining a new class:
The code is as follows: |
|
If (class_exists ('someclass ')! = True) { // Put class SomeClass here } If (class_exists ('someclass ')! = True) { // Put class SomeClass here } |
2. duplicate files containing the same class:
For example, for a class file some_class.php
The code is as follows: |
|
Include "some_class.php "; Include "some_class.php "; |
In B. php
The code is as follows: |
|
Include "a. php "; Include "some_class.php "; Include "a. php "; Include "some_class.php ";
|
An error is reported.
Solution: replace all the preceding include with include_once.
3. this class is the built-in class in the PHP class library.
Judgment method: write in an empty file
The code is as follows: |
|
Class Com { } ?> |
The prompt "Cannot redeclare class Com" indicates that this class is the built-in class of PHP. Cannot be used.
In addition, avoid using too popular class names, such as Com. this class may be normal in Linux, but cannot run in Windows.
Remember the solution found on the Internet, which may be useful in some occasions.
The code is as follows: |
|
If (! Class_exists ('pagemodule ')){ Require_once (PATH_site. 'fileadmin/scripts/class. page. php '); }
|
The above method does not apply to the php _ autoload class loading method, but it can solve the problem, __autoload is automatically loaded. we only need to find the same class name and rename it.
Classes _ autoload status automatically loads the class. Today, a good program does not know how to prompt Fatal error: Cannot redeclare class at runtime. it seems that the class has been repeatedly defined...