首先,我对此进行了研究。但我找不到任何解决方案。 这是我想做的。 我有多个具有相同函数名称的文件,在测试过程中我没有指定该函数并保持简单。
将包含多个文件,但由于该函数已被声明,我收到 fatal error 无法重新声明函数...,先前在...中声明
我知道如何避免该 fatal error ,但由于我包含具有相同功能的多个文件,因此无法修复它。
我的问题是,这符合逻辑吗?还是我应该重新思考并做一些不同的事情。
我的 friend 告诉我这可以用对象等来处理。
感谢您的任何建议。
最佳答案
功能相同吗?在这种情况下,您可以通过用 function_exists()
包装函数声明来“破解”问题。条款,请参阅How to avoid fatal error Cannot redeclare function in PHP :
<?php
if (!function_exists('utility')) {
function utility() {
// ...
}
}
?>
或者,也许值得您花时间阅读 namespaces :
In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions:
- Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
- Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code. (source)
关于php - 复制功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6789320/