PHP 相当于 send 和 getattr?
如果 Ruby 被邀请参加一个聚会并带来:
foobarobject.send('foomethod')
.. 而 Python 被邀请参加同一个聚会并带来:
getattr(foobarobject, 'foomethod')()
.. PHP 必须为聚会带来什么?
额外问题:如果 Ruby 和 Python 嫉妒 PHP 的青睐,他们会在 PHP 文档中搜索哪些英文术语,以便在背后谈论 PHP?
If Ruby gets invited to a party and brings:
foobarobject.send('foomethod')
.. and Python gets invited to the same party and brings:
getattr(foobarobject, 'foomethod')()
.. what does PHP have to bring to the party?
Bonus question: If Ruby and Python got jealous of PHP's party-favors, what English terms would they search for in PHP's documentation in order to talk about it behind PHP's back?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PHP 带来了这个:
...以及可乐和薯片。
编辑:
虽然上述术语是
变量变量
手册中没有专门讨论对对象执行此操作。 但是,您可以使用
call_user_func
实现相同的效果:PHP brings this:
... and the coke and chips.
EDIT:
Although the term for the above is
variable variables
there is nothing specifically talking about doing it to an object in the manual. However, you can achieve the same thing withcall_user_func
:使用变量来保存方法名称。 与 Paolo 的第一个例子几乎相同,但可能不是那么明显,除非你了解它。
您还获得了反射类。
Using variable to hold the method name. Pretty much the same as Paolo's first example, but maybe not so obvious unless you know about it.
You also got the Reflection classes.