你有三種選擇,真的。
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