PHP Critical fatal error handling: PHP Fatal Error:cannot redeclare class or function

Source: Internet
Author: User
Tags php class
This article mainly describes the PHP fatal error handling: PHP Fatal Error:cannot redeclare class or function, the need for friends can refer to the following

1. Error type: PHP fatal error

Error type:php Fatal error
Fatal Error:cannot Redeclare (a) (previously declared in (b)) and (c) on line (d)

2. Error Description:

The error report indicates that you are attempting to redefine a function that has already been defined, where

A----represents a duplicate defined function name;
b----The file name and line number of the first time the function is defined;
C----The name of the file when the function is defined for the second time;
D----The line number when the function is defined the second time.

3, Causes and solutions:

Reason: You use the same name to define a function two times in a row, for example

function MyFunction () {}
function MyFunction () {}

The results are as follows

Fatal error:cannot redeclare myfunction () (previously declared in (path): 2) in (path) on line 1

Solve:

Find the function you've already declared and see what it takes to cause you to define it again. If you simply forget that you have defined it before, then delete one of the declarations. For example, your script file is out of order and you may be using a large number of functions such as include (), which will make it difficult for you to clear your mind from the confusing code. However, if your PHP version is newer (PHP 5.3.8+) It seems that you can use namespaces to solve the situation where it is necessary to have a duplicate definition function.

4 PHP Critical fatal error handling examples are explained below


1) repeated declarations of two classes of the same name in the same file:

For example:

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

if (class_exists (' someclass ') = True) {  //put class SomeClass here}

2) repeat contains the same class file:

For example: For a class file some_class.php, in a.php

Include "some_class.php";

Include "some_class.php";

In the b.php

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

<?php 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

if (!class_exists (' Pagemodule ')) {require_once (path_site. ' fileadmin/scripts/class.page.php ');}

The above is the whole content of this article, I hope that everyone's study has helped.


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.