Php wordpress从空do_操作获取变量
如何获得$secret值?因为开发人员在使用do_action时没有传递任何变量,所以您的变量超出范围Php wordpress从空do_操作获取变量,php,wordpress,function,arguments,closures,Php,Wordpress,Function,Arguments,Closures,如何获得$secret值?因为开发人员在使用do_action时没有传递任何变量,所以您的变量超出范围 /* I cant change this part (plugin core files) */ function test123(){ $secret = "hoho"; //i want to get this variable do_action('custom'); //developer didnt pass any variable here } /* I ca
/* I cant change this part (plugin core files) */
function test123(){
$secret = "hoho"; //i want to get this variable
do_action('custom'); //developer didnt pass any variable here
}
/* I cant change this part (plugin core files) */
add_action('custom',function() use ( $secret ) {
echo $secret; //didn't work
});
test123();
预期产量
ini_set('display_errors', 1);
error_reporting(E_ALL);
add_action('custom',function() use ( $secret ) {
echo gettype($secret); //didn't work
});
当你更新原始的“插件/主题”时,你可以很容易地找到你的更改的位置(通过搜索评论文本),并将它们应用到新版本的“插件/主题”中。这可能像处理所有文件一样简单,但您更改的文件除外(如果没有更改),等等
正如我所说,这并不理想,但它会起作用。这只取决于你有多想实现它,以及你有多愿意维护它
另请参见
在用户定义的函数中引入了局部函数作用域。默认情况下,函数中使用的任何变量都限制在局部函数范围内
您可以这样使用:
<br />
<b>Notice</b>: Undefined variable: secret in <b>...</b> on line <b>...</b><br />
NULL
/* START CUSTOM CODE */
希望这能奏效 显然,
$secret
只存在于函数体test123
之外(如问题所示),而不存在于函数范围之外。因此,当你把它交给闭包时,它是未定义的,当你回应说它什么都不是。嗨,我忘了提到我不能更改test123()部分嗨,我忘了提到我不能更改test123()部分
/* START CUSTOM CODE */
function test123() {
$secret = 'hoho';
do_action( 'custom', $secret );
}
add_action('custom',function( $secret ) {
echo $secret;
});
test123();