I use PHP __autoload State automatic loading class, today good program do not know how to prompt fatal Error:cannot Redeclare class, see is repeating the definition of class, let me analyze the solution.
Error hints
Fatal Error:cannot Redeclare class ....
From a literal point of view, the explanation is to repeat the definition of the class, to find their own code, because there is a class of the same name caused by the modification of the class name is good.
Cause analysis
1. Repeated declarations of two classes of the same name in the same file:
For example:
The code is as follows |
Copy Code |
Class Foo {}
Some code here
Class Foo {} ?> |
There will be an error in the second Foo place.
Workaround: Remove the second foo, or rename it.
To prevent duplicate definitions, you can determine whether the class already exists when you define 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 contains the same class file:
For example: For a class file some_class.php, in a.php
The code is as follows |
Copy Code |
Include "some_class.php"; Include "some_class.php"; |
In the b.php
The code is as follows |
Copy Code |
Include "a.php"; Include "some_class.php"; Include "a.php"; Include "some_class.php";
|
will be an error.
Workaround: Replace all of the above include with include_once
3. This class is a class built into the PHP class library.
How to determine: write in an empty file
The code is as follows |
Copy Code |
Class Com {
} ?> |
This time prompt cannot redeclare class Com, this class is PHP built-in class. cannot be used.
In addition, to avoid using a class name that is too popular, such as COM, this class may be normal for Linux use and cannot be run in a Windows environment.
Remember a solution found on the Internet, may be useful in some situations, first remember
The code is as follows |
Copy Code |
if (!class_exists (' Pagemodule ')) { Require_once (path_site. ' fileadmin/scripts/class.page.php '); }
|
The above approach does not apply to the PHP __autoload class loading method, but has been able to solve the problem, __autoload is automatically loaded we just have to find the same class name and then rename it.
http://www.bkjia.com/PHPjc/632073.html www.bkjia.com true http://www.bkjia.com/PHPjc/632073.html techarticle I use PHP __autoload State automatic loading class, today good program do not know how to prompt fatal Error:cannot Redeclare class, see is repeating the definition of class, the next ...