Fatal error: Cannot redeclare class Cause Analysis and Solution

Source: Internet
Author: User
Tags autoload

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.

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.