PHP 相当于 send 和 getattr?

发布于 2024-07-16 22:10:34 字数 306 浏览 3 评论 0原文

如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以往的大感动 2024-07-23 22:10:34

PHP 带来了这个:

$foobarobject->{"foomethod"}();

...以及可乐和薯片。

编辑

虽然上述术语是变量变量手册中没有专门讨论对对象执行此操作。 但是,您可以使用 call_user_func 实现相同的效果:

call_user_func(array($foobarobject, "foomethod"));

PHP brings this:

$foobarobject->{"foomethod"}();

... 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 with call_user_func:

call_user_func(array($foobarobject, "foomethod"));
演出会有结束 2024-07-23 22:10:34

使用变量来保存方法名称。 与 Paolo 的第一个例子几乎相同,但可能不是那么明显,除非你了解它。

$method = "foomethod";
$foobarobject->$method();

您还获得了反射类

$method = new ReflectionMethod('Foobar', 'foomethod');
$method->invoke(null);

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.

$method = "foomethod";
$foobarobject->$method();

You also got the Reflection classes.

$method = new ReflectionMethod('Foobar', 'foomethod');
$method->invoke(null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文