In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "errors in PHP: php Fatal error: the causes and handling methods of Cannot redeclare class or function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
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:
The error report indicates that you are trying to redefine a function that has been defined, where
A Mui-indicates the name of a function that is repeatedly defined
The file name and line number when the function was defined for the first time
CmurmurMel-the file name when the function was defined for the second time
DmurmurMel-the line number when the function was defined for the second time.
3. Reasons and solutions:
Reason: you define a function with the same name twice 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
Resolve:
Find the function that has been declared and see what needs to cause you to define it again. If you simply forget that it has been defined before, delete one of the statements. For example, your script files are very messy, and you may use a lot of functions such as include (), which will make it difficult for you to sort out your thoughts from the cluttered code. However, if you have a newer version of PHP (PHP 5.3.8 +), it seems that you can use namespaces to solve situations where there is indeed a need to redefine functions.
4 examples of PHP serious fatal error handling are as follows
1) A class with the same name has been declared twice in the same file:
For example:
There will be an error at the second Foo.
Solution: remove the second Foo, or rename it.
To prevent duplicate definitions, you can determine whether a new class already exists when you define it:
If (class_exists ('SomeClass')! = true) {/ / put class SomeClass here}
2) repeatedly include 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 b.php
Include "a.php"
Include "some_class.php"
Include "a.php"
Include "some_class.php"
You'll get the wrong report.
Solution: replace all the above include with include_once
3) this class is built-in in the PHP class library.
Judgment method: write in an empty file
At this point, prompt Cannot redeclare class Com to indicate that this class is the built-in class of PHP. It can't be used.
In addition, avoid using too popular class names, such as Com, which may be normal in Linux but not work in Windows.
Remember another solution found online, which may be useful in some situations. Remember first.
If (! class_exists ('pageModule')) {require_once (PATH_site.'fileadmin/scripts/class.page.php');}
The above method does not apply to the use of php _ autoload class loading method, but the problem can be solved. _ _ autoload is loaded automatically. We just need to find out the same class name and rename it.
This is the end of the content of "errors in PHP: php Fatal error: the causes and handling of Cannot redeclare class or function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.