PHP中何时使用变量?
我已经用 PHP 进行开发有一段时间了,但我仍然没有遇到必须使用可变变量的任务。 谁能给我举例说明使用它们是个好主意吗? 或者它们被包含在语言中只是为了好玩?
I've been developing in PHP for a while now, and I still have not had a task where I've had to use variable variables. Can anyone give me examples where using them is a good idea ? Or were they included in the language just for fun ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我通常会在代码味道不好的地方找到它们。 也许引用静态配置变量等...但是为什么通常的关联数组不是更好的解决方案。 看起来像是一个即将发生的安全漏洞。
我想您也许能够在模板中有效地使用它们。
I generally find them in places where the code smells bad. Maybe references a static configuration variable etc... But why wouldn't the usual associative array have been a better solution. Seems like a security hole waiting to happen.
I suppose you might be able to use them in templates effectively.
切勿使用它们; “数组”始终是更好的解决方案。
Never use them; an "array" is always a better solution.
首先,如果您将用户输出用于这些目的,这将是一个巨大的安全问题。 内部是这里唯一有效的用途。
鉴于此,我想它是用于循环遍历各种变量或将变量作为参数发送之类的事情。
First off, it'd be a huge security concern were you to use user output for these purposes. Internals are the only valid use here.
Given that, I imagine it's for things like looping through various variables, or sending variables as parameters.
我不得不使用它们的一种情况是 URI 处理,尽管这种技术可能已经过时,而且我承认我已经很长时间没有使用它了。
假设我们想要从脚本中提取
domain.tld/controller/action/parameter/s
格式的 URI。 我们可以使用以下命令删除脚本名称:要从中提取控制器、操作和参数值,我们必须使用路径分隔符“/”分解字符串。 但是,如果我们有前导或尾随分隔符,则在爆炸时我们将得到空数组值,因此我们应该从字符串的开头和结尾修剪这些值:
我们现在可以将路径分解为数组:
$uri_data[ 0]
现在包含我们的控制器名称,$uri_data[1]
包含操作名称,数组中除此之外的值是应传递给操作方法的参数。因此,既然我们有了这些名称,我们就可以将它们用于很多事情。 如果您将控制器保存在相对于站点根目录的非常特定的目录中,则可以使用此信息来
require_once
控制器类。 此时,您可以实例化它并使用可变变量调用它:在这种方法中需要注意很多安全问题,但这是我见过的使用可变变量的一种方法。
免责声明:我并不是建议您实际使用此代码。
One situation where I've had to use them is URI processing, although this technique might be dated, and I admittedly haven't used it in a long time.
Let's say we want to pull the URI from the script in the format
domain.tld/controller/action/parameter/s
. We could remove the script name using the following:To extract the controller, action, and parameter values from this we're going to have to explode the string using the path delimiter '/'. However, if we have leading or trailing delimiters, we'll have empty array values upon explosion, so we should trim those from the beginning and end of the string:
We can now explode the path into an array:
$uri_data[0]
now contains our controller name,$uri_data[1]
contains the action name, and values in the array beyond that are parameters that should be passed to the action method.So, now that we have these names, we can use them for a number of things. If you keep your controllers in a very specific directory relative to the site root, you can use this information to
require_once
the controller class. At that point, you can instantiate it and call it using variable variables:There are a lot of security gotchas to look out for in this approach, but this is one way I've seen to make use of variable variables.
DISCLAIMER: I'm not suggesting your actually use this code.
我找到了一个相当好的文件..
现在我要求用户说出他想要哪个文件 php 或/和 html..
输出:
当我试图确定静态变量的范围时,我已经与自己划过界限了
self::$file;
像这样我记得我可以使用可变变量self::$$file;
将被解释为self::$php;
I've found a quite good one..
now i asked the user to say which file he wants php or/and html..
output:
I've crossed myself with this when trying to scope static variables
self::$file;
like this then I remembered I could use variable variablesself::$$file;
which will be interpreted asself::$php;
语言中包含不应该用 bargepole 触及的功能并不罕见(我什至问过不久前有一个关于它的问题),而变量变量可能是属于此类别的结构之一。 仅仅因为一种语言包含某种功能并不意味着您必须使用它。
有时他们可能会解决问题(毕竟在实践中很少使用递归,但没有人会认为这不是一个必要的构造),但一般来说,任何语言功能都会模糊你的代码正在做什么,并且变量变量会公然落入应极其谨慎地对待这一类别。
It's not uncommon for languages to include features you shouldn't touch with a bargepole (I even asked a question about it a while back), and variable variables are probably one of those constructs that fall into this category. Just because a language contains a feature doesn't mean to say you have to use it.
There may be occasions when they solve a problem (after all recursion is rarely used in practice but no-one would argue that's not an essential construct) but in general any language feature that obscures what your code is doing, and variable variables defiantly fall into this category, should be treated with extreme caution.
除非您正在使用多深度变量(如果您不做任何奇特的事情,则不需要),否则您可能不需要它们。 即使如此,您也可能找到另一种方法来写下相同的内容并仍然得到相同的结果。 不过,使用它们可能会更短(在某些情况下甚至更容易理解),所以我很高兴它是语言的一部分。
Unless you are working with multi-depth variables (which you won't need to if you are not doing anything fancy) you will probably don't need them. Even then, you can probably find another way to write down the same thing and still get the same result. It can be shorter (and in some cases even easier to understand) to use them though, so I for one am glad that it is part of the language.
我还没有发现变量有很多用途,但是只要您所做的事情很清楚,使用变量作为方法就可以很方便。 例如,在一个简单的 REST 服务中,您可能会这样做:
有些人会认为您可以使用
switch
语句(您可以)同样出色地完成此操作,但这种方式有助于扩展性(如果您决定)添加另一个方法类型)并维护在资源上应用方法的抽象。I haven't found many uses for variable variables but using variables for methods can be handy, as long as what you're doing is clear. For example in a simple REST service you might do something like this:
Some would argue you could do this equally well with a
switch
statement (which you could) but this way lends itself to extensibility (if you decide to add another method type) and maintains the abstraction of applying a method on a resource.