你不能重新定義全局函數(outside of runkit,但這是巫毒),所以您的選擇仍然使用一個類驅動的解決方案,或者匿名函數驅動的解決方案如下。
你可以寫一個函數來組裝所有包含的功能爲呼叫隊列(這就是運行隊列另一個匿名函數):
function queueFunctions(array $functions){
return function()
use ($functions) {
foreach($functions as $function) {
$function();
}
};
}
而且從每個文件作爲返回匿名函數你所描述的,而不是重新定義一個全局函數:
file1.php
return function() {
echo 'Hello';
};
file2.php
return function() {
echo 'World';
};
然後,您可以:
// both
$function = queueFunctions([
include('file1.php'),
include('file2.php'),
]);
$function(); // HelloWorld
// one of them
$function = queueFunctions([
include('file2.php'),
]);
$function(); // World
// repeated calls
$function = queueFunctions([
include('file2.php'),
include('file1.php'),
include('file2.php'),
]);
$function(); // WorldHelloWorld
來源
2012-07-13 06:18:49
Dan
什麼是你想實現什麼?給我們一些背景將允許我們給出最合適的解決方案:) – F21 2012-07-13 05:28:43
爲什麼不測試它 – 2012-07-13 05:29:42
不,這是不可能的,我甚至不知道它會對它有用。 – deceze 2012-07-13 05:30:57