Fatal Error:cannot Redeclare class cause analysis and Solution _php Tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.