你是对的,这是非常主观的问题,但我可能会使用你的两种选择.
你有一个类(比如Helper)有__call()(和/或__callStatic(),如果你使用PHP 5.3)magic methods,当一个未定义的[静态]方法被调用时它会加载相应的帮助文件和执行辅助函数.请记住,虽然包含文件会降低性能,但我相信您在文件组织方面获得的好处远远超过了微小的性能影响.
一个简单的例子:
class helper {
function __callStatic($m, $args) {
if (is_file('./helpers/' . $m . '.php')) {
include_once('./helpers/' . $m . '.php');
return call_user_func_array($m, $args);
}
}
}
helper::isFilePhp(/*...*/); // ./helpers/isFilePhp.php
helper::getCurrentFolder(/*...*/); // ./helpers/getCurrentFolder.php
您可以通过向每个类添加__call [Static]()魔术方法并在您的文件夹/文件结构中实现一些逻辑来进一步优化此代码段,甚至还有几种类型的帮助程序(文件夹,文件)等等.辅助文件/功能.