rov*_*red 6 php scope function
有没有办法限制php文件中的非类函数的范围,并使它们只能在它所在的php文件中访问?就像C可以使用带有函数的静态关键字来实现这一点.在php中,静态似乎只适用于类.我想隐藏只能由文件中的函数访问的辅助函数.谢谢.
我能想到的最接近的解决方案是:
<?php
call_user_func( function() {
//functions you don't want to expose
$sfunc = function() {
echo 'sfunc' . PHP_EOL;
};
//functions you want to expose
global $func;
$func = function() use ($sfunc) {
$sfunc();
echo 'func' . PHP_EOL;
};
} );
$func();
?>
Run Code Online (Sandbox Code Playgroud)
但你必须调用类似的函数$func()而不是func(). 问题是当您重新分配$func给其他值时它会中断。
$func = 'some other value';
$func(); //fails
Run Code Online (Sandbox Code Playgroud)
当然,您可以创建包装函数:
function func() {
$func();
}
Run Code Online (Sandbox Code Playgroud)
这样你就可以这样称呼它func(),但是重新分配的问题仍然存在:
$func = 'some other value';
func(); //fails
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1086 次 |
| 最近记录: |