PHP Fatal error handling: php Fatal error: Cannot redeclare class or function, fatalredeclare
1. Error Type: PHP fatal error
Error type: PHP Fatal error
Fatal error: Cannot redeclare (a) (previusly declared in (B) in (c) on line (d)
2. Error description:
This error report indicates that you are planning to redefine the defined functions.
A ---- indicates the name of the function that is repeatedly defined;
B ---- name and row number of the file when the function is defined for the first time;
C ---- the file name for the second definition of the function;
D ---- the row number when the function is defined for the second time.
3. Reasons and solutions:
Cause: You define a function using the same name twice in a row, for example
Function myFunction (){}
Function myFunction (){}
The result is as follows:
Fatal error: Cannot redeclare myfunction () (previously declared in (path): 2) in (path) on line 1
Solution:
Find the declared function and see what causes you to define it again. If you simply forget that you have previously defined the statement, delete it. For example, if the arrangement of your script files is exceptionally messy and you may use a large number of include () functions, it will make it difficult for you to clarify your ideas from the chaotic code. However, if your PHP version is newer (PHP 5.3.8 +), you can use a namespace to solve the need for repeated function definitions.
4. Examples of PHP severe and fatal error handling are as follows:
1) duplicate the class with the same name in the same file:
For example:
<?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:
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
Include "some_class.php ";
Include "some_class.php ";
In B. php
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
<?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.
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.