PHP - 解决闭包和反射问题
我需要创建必须由应用程序自动实现的动态面包屑。所以我在 URL 中有以下结构用于导航: nav=user.listPMs.readPM&args=5
那么我可以有一个函数文件,其唯一目的是定义 user.listPMs.readPM 函数本身:
文件:nav/user.listPMs.readPM.php< /strong>
function readPM($msgId)
{
/*code here*/
}
当然,这最终会扰乱全局范围,因为我没有用类或名称空间来包装函数。这里最好的解决方案似乎是命名它,毫无疑问,对吗?但我还想到了另一个:
file: nav/user.listPMs.readPM.php
return function($msgId)
{
/*code here*/
};
是的,就这么简单,该文件只是返回一个匿名函数。我认为这太棒了,因为我不需要关心它的命名 - 因为我已经正确命名了包含它的文件,创建了一个用户函数,但必须命名它似乎是多余的。然后在索引中我会有这个小肮脏的把戏:
file: index.php
if($closure = @(include 'nav/'.$_GET['nav']))
{
if($closure instanceof Closure)
{
$obj = new ReflectionFunction($closure);
$args = explode(',',@$_GET['args']);
if($obj->getNumberOfParameters($obj)<=count($args))
call_user_func_array($closure,$args);
else
die('Arguments not matching or something...');
} else {
die('Bad call or something...');
}
} else {
die('Bad request etc.');
}
甚至不需要提及面包屑可以通过解析 $_GET[' 中的值来很好地构建。 nav'] 变量。
那么,您认为这个问题有没有更好的解决方案呢?您是否找到了另一种探索闭包和/或反射的方法?
I needed to create dynamic breadCrumbs that must be realized automatically by the application. So I have the following structure in the URL for navagation:
nav=user.listPMs.readPM&args=5
then i could have a function-file whose sole purpose would be to define the user.listPMs.readPM function itself:
file: nav/user.listPMs.readPM.php
function readPM($msgId)
{
/*code here*/
}
Of course this ends up cluttering the global scope since i'm not wrapping the function withing a class or using namespaces. The best solution here seems to be namespacing it, no doubt right? But I also thought of another one:
file: nav/user.listPMs.readPM.php
return function($msgId)
{
/*code here*/
};
Yep, that simple, the file is simply returning an anonymous function. I think this is amazing because i don't need to care about naming it - since i've already properly named the file that contains it, creating a user function and yet having to name it would seem just redundant. Then in the index I would have this little dirty trick:
file: index.php
if($closure = @(include 'nav/'.$_GET['nav']))
{
if($closure instanceof Closure)
{
$obj = new ReflectionFunction($closure);
$args = explode(',',@$_GET['args']);
if($obj->getNumberOfParameters($obj)<=count($args))
call_user_func_array($closure,$args);
else
die('Arguments not matching or something...');
} else {
die('Bad call or something...');
}
} else {
die('Bad request etc.');
}
Don't even need to mention that the breadCrumbs can be nicely built latter just by parsing the value within the $_GET['nav'] variable.
So, what do you think, is there a better solution to this problem? Have you found another way to explore Closures and/or Reflection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我喜欢这个基本想法。但实施情况却非常糟糕。想象一下,我设置了
nav=../../../../../../etc/passwd
。这将(取决于您的服务器配置)允许我访问您的密码文件,这当然不好。I like the basic idea. But the implementation is pretty much terrible. Imagine that I set
nav=../../../../../../etc/passwd
. That would (depending on your server configuration) allow me to access your password file, which certainly is no good.