本当に3つの選択肢があります。変数あなたがする必要があるだろう第二の例では
// 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();
}
へ
// 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();
アサイン機能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