jQuery中,你可以寫類似php回調(如jQuery)?
$(selector).bind('click', function(e) {
// something was true - do these actions.
});
我在想,如果在PHP,你可以沒有使用eval類似的東西。
這樣的事情?我知道這不會工作順便說一句。
class act{
public function bind($pref, $callback) {
if($pref == 'something') {
// return and perform actions in $callback?
eval($callback);
}
}
}
你們有些人可能會問這是什麼需要?那麼我試圖簡化我的代碼,而不使用這麼多的if語句。
$act = new act;
$act->bind('something', function(e) {
echo 'this was the php code to run in the callback';
});
上面的代碼可以避免使用一堆if語句。
$act = new act;
if($act->bind('something')) {
// bind returned true so do this?
}
我知道你可以使用ob_get_contents來返回evaled代碼,但是ewww。
ob_start();
eval('?> ' . $callback . '<?php ');
$evaled = ob_get_contents();
ob_end_clean();
return $evaled;
檢查我張貼在我的答案例如,它正是你想要什麼 – dynamic 2011-03-17 22:31:04