如何将 is_callable 与 __call 一起使用?
我使用 PHP 5.3,它引入了闭包。因为我现在在整个应用程序(和框架)中都可以使用闭包,所以我使用 is_callable 来查看 $callback
是什么类型的处理程序。
如果 $callback
is_callable
,我就足够了解并使用该函数/方法/闭包。如果它不可调用并且是一个字符串,则它可能是 $this
类中的方法的名称。它可能存在,也可能不存在。如果我让它由 PHP 决定,它会“抛出”一个很好的致命错误(我喜欢那些)。
但我想使用 mix-ins 这意味着我需要神奇的方法 __call.
__call
非常酷,因为它可以在实际函数调用之前包含逻辑。 但是...如果__call
之后被调用的方法不存在会发生什么?当然,我可以抛出异常,但直到事后我们才会知道。
现在的问题是 is_callable
的用处,因为在实现 __call
后,一切都会返回 true,因为一切都有一个后备(是 __call
)。
有没有办法同时拥有:动态方法和有用的 is_callable
?
我希望看到某种可缓存的魔术方法 __is_callable
,当调用 is_callable(array($object, $method))
时,PHP 将“咨询”该方法。
在 php.net 上,我找不到像我一样对此感到困惑的人。不可能是这样!如果你使用__call
,你就不能再使用is_callable
了!?
我说得有道理吗?
PS. 我已经研究过 method_exists
但这还不够好(即使我可以过滤掉所有闭包和全局函数),因为它对于 private 和 protected 会返回 true方法以及公共。
编辑
我制作了 一个“更好”的 is_callable 来检查用于混合,但我认为它相当昂贵。
I use PHP 5.3, which introduces closures. Because I now have closures available throughout my app (and framework), I use is_callable to see what kind of handler $callback
is.
If $callback
is_callable
, I know enough and use that function/method/closure. If it's not callable and it's a string, it's probably the name of a method in $this
class. It might exist and it might not. If I let it up to PHP, it will 'throw' a nice fatal error (I like those).
But I want to use mix-ins which means I need the magic method __call
. __call
is very cool, because it (can) contain(s) logic before the actual function call. BUT... what happens if after __call
the called method doesn't exist? I can throw an exception, sure, but we won't know until after.
The problem is now the usefulness of is_callable
, because after implementing __call
, everything will return true, because everything has a fallback (being __call
).
Is there a way to have both: dynamic methods and useful is_callable
?
What I'd love to see is some sort of cachable magic method __is_callable
that PHP will 'consult' when is_callable(array($object, $method))
is called.
On php.net, I can't find anyone who is as bumbed about this as me. This can't be it! If you use __call
, you can't use is_callable
anymore!?
Am I making any sense?
PS. I've looked into method_exists
but that's just not good enough (even if I can filter out all closures and global functions) because it will return true for private and protected methods as well as public.
edit
I made a 'better' is_callable that checks for mix-ins but I think it's pretty expensive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个想法:检查 get_class_methods() 中是否存在方法怎么样?该函数负责处理范围问题。
第二个想法:如果您要查找的函数在类中不存在,您还可以检查(使用
method_exists
)类是否包含__call
魔术方法,如果不存在显然调用不存在的函数是非法的。如果类包含__call
方法,调用者很可能知道他在做什么。这些只是我的想法,我认为 Python 在这类事情上会更好。
First idea: how about checking if method exists in
get_class_methods()
? This function takes care of scope issues.Second idea: if function you're looking for doesn't exist in a class, you could also check (using
method_exists
) if class contains__call
magic method, if it doesn't than obviously call to a non-existing function is illegal. If class contains__call
method, it's likely that caller knows what he's doing.These are just my thoughts, I assume Python to be better in this kind of things.