PHP serious fatal error handling:php Fatal error: Cannot redeclare class or function

墨辰丷
Release: 2023-03-28 06:48:01
Original
2657 people have browsed it

This article mainly introduces PHP serious fatal error handling: php Fatal error: Cannot redeclare class or function, friends in need can refer to it

1. Error type: PHP fatal error

Error type: PHP Fatal error
Fatal error: Cannot redeclare (a) (previously declared in (b)) in (c) on line (d)

2. Error description:

This error report indicates that you are trying to redefine an already defined function, where

a---- indicates the name of a repeatedly defined function;
b----The file name and line number when the function is defined for the first time;
c----The file name when the function is defined for the second time;
d----No. The line number when the function is defined a second time.

3. Reasons and solutions:

Cause: You use the same name twice in a row to define a function, such as

function myFunction() {}
function myFunction(){}

The result is as follows

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

Solution:

Find the function that has been declared and see what needs caused you to define it again. If you simply forgot that it was defined before, just delete one of the declarations. For example, your script files are arranged in an extremely confusing manner, and you may use a large number of include() and other functions, which will make it difficult for you to sort out ideas from the confusing code. However, if your PHP version is relatively new (PHP 5.3.8), it seems that you can use namespaces to solve the situation where it is really necessary to repeatedly define functions.

4 PHP serious fatal error handling and resolution examples are as follows


1) A class with the same name is declared twice in the same file:

For example:

<?php 
class Foo {} 
// some code here 
class Foo {} 
?>
Copy after login

An error will be reported at the second Foo.

Solution: Remove the second Foo, or rename it.

In order to prevent repeated definitions, you can determine whether the class already exists when defining a new class:

if(class_exists(&#39;SomeClass&#39;) != true) 
{ 
 //put class SomeClass here 
}
Copy after login

2) Repeat Contain the same class files:

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

include "some_class.php";

include "some_class .php";

In b.php

include "a.php";
include "some_class.php";
include "a.php";
include "some_class.php";

will report an error.

Solution: Replace all the above include with include_once

3) This class is a built-in class in the PHP class library.

Judgment method: Write

<?php 

class Com 
{ 
 

} 
?>
Copy after login

in an empty file. At this time, it prompts Cannot redeclare class Com, indicating that this class is built-in in PHP. kind. Out of service.

In addition, avoid using class names that are too popular, such as Com. This class may be normal in Linux, but cannot run in Windows environment.

Remember another solution found online, it may be useful in some situations, first remember

if (!class_exists(&#39;pageModule&#39;)){ 
require_once(PATH_site.&#39;fileadmin/scripts/class.page.php&#39;);
}
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP interceptionfatal error example

php Method to capture Fatal error

php Fatal error of variable variable: Cannot use [] for reading

The above is the detailed content of PHP serious fatal error handling:php Fatal error: Cannot redeclare class or function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!