您可以将匿名函数与PHP的use
这样的陈述:
function mycars($mycarsclass){
$args = array(
\'car1\' => \'Volvo\',
\'car2\' => \'Toyota\'
);
$mycarsclass->add_node($args);
}
add_action(\'mycarsaction\', function () use ($mycarsclass) {
mycars($mycarsclass);
});
do_action(\'mycarsaction\');
或者声明一个单独的函数并传递:
function mycars($mycarsclass){
$args = array(
\'car1\' => \'Volvo\',
\'car2\' => \'Toyota\'
);
$mycarsclass->add_node($args);
}
function mycars_mycarsclass() {
$mycarsclass = new mycarsclass();
mycars($mycarsclass);
}
add_action(\'mycarsaction\', \'mycars_mycarsclass\');
do_action(\'mycarsaction\');