I used 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: |
Copy code |
<? Php 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: |
Copy code |
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: |
Copy code |
Include "some_class.php "; Include "some_class.php "; |
In B. php
The Code is as follows: |
Copy code |
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: |
Copy code |
<? Php 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: |
Copy code |
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.