Fatalerror: Cannotredeclareclass cause analysis and solution _ PHP Tutorial

Source: Internet
Author: User
Fatalerror: Cannotredeclareclass cause analysis and solution. I used the php _ autoload status to automatically load classes. Today, a good program does not know how to prompt Fatalerror: Cannotredeclareclass at runtime. the class is already defined, I am using 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:
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:
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:

Include "some_class.php ";
Include "some_class.php ";

In B. php

The code is as follows:


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:
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:


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.

Classes _ autoload status automatically loads the class. Today, a good program does not know how to prompt Fatal error: Cannot redeclare class at runtime. it seems that the class has been repeatedly defined...

Related Article

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.