你有三种选择,真的。
function_exists()
// this will check for the function's existence before trying to declare it
if(!function_exists('cool_func')){
function cool_func(){
echo 'hi';
}
}
// business as usual
cool_func();
分配功能到可变
// this will automatically overwrite any uses of $cool_func within the current scope
$cool_func = function(){
echo 'hi';
}
// call it like this
$cool_func();
/* WARNING: this does not work */
/* eval() operates in the global space */
namespace first {
eval($source_code);
cool_func();
}
namespace second {
eval($source_code);
cool_func();
}
// like this too
first\cool_func();
second\cool_func();
/* this does work */
namespace first {
function cool_func(){echo 'hi';}
cool_func();
}
namespace second {
function cool_func(){echo 'bye';}
cool_func();
}
随着你将需要第二个例子eval()
每次你需要使用$cool_func
范围内的DB一次代码,见下图:
eval($source_code);
class some_class{
public function __construct(){
$cool_func(); // <- produces error
}
}
$some_class = new some_class(); // error shown
class another_class{
public function __construct(){
eval($source_code); // somehow get DB source code in here :)
$cool_func(); // works
}
}
$another_class = new another_class(); // good to go
我们可以看到你实际上是试图做? – DevDonkey
@DevDonkey我可以看到完美的代码!你不是通灵吗?获取它:-) – MonkeyZeus
如果您发布了一些代码,我们可能会提供帮助,否则重命名一个函数或将其移动到一个类中可能会失败。你也可以使用'function_exists' – DaOgre