魔术 __call 方法的字符串匹配
我有一组名为 getThing($what, $extra_args)
的方法(get 始终出现在名称中)
...除了调用它们的正常方式之外,我也希望能够像这样调用它们:
getWhatThing()
=>;return getThing($what, $extra_args);
WhatThing()
=>;echo getThing($what, $extra_args);
Thing('what')
=>;echo getThing('what', $extra_args);
如果所有这些都失败,则会抛出方法不存在错误...
我如何在 __call()
中执行此操作? 我知道我应该避免使用魔法方法,但我想让那些使用我的 API 的人的生活变得更轻松:)
谢谢
I have a set of methods named like getThing($what, $extra_args)
(get is always present in the name)
...and besides the normal way of calling them, I also want to be able to call them like:
getWhatThing()
=>return getThing($what, $extra_args);
WhatThing()
=>echo getThing($what, $extra_args);
Thing('what')
=>echo getThing('what', $extra_args);
if all these fail throw the Method doesn't exist error...
How can I do this from within __call()
?
I know I should avoid magic methods, but I want to make life easier for the people who will use my API :)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你问的是无意义的..
您的所有方法都应该
返回 $var
。此时,如果您想回显它,您需要明确执行以下操作:
What you are asking is a non sense..
All your methods should just
return $var
.At that point if you want to echo it you need to excplicty do:
PHP.net 文档的评论中的代码 __call 对您来说应该是一个很好的起点,但是您需要向 switch 部分添加更多匹配项。
The code in this comment on PHP.net's documentation for __call should be a good starting point for you, but you'll need to add more matches to the switch section.