[英]How to use is_callable with __call?
I use PHP 5.3, which introduces closures. 我使用PHP 5.3,它引入了闭包。 Because I now have closures available throughout my app (and framework), I use is_callable to see what kind of handler
$callback
is. 因为我现在在我的应用程序(和框架)中都有可用的闭包,所以我使用is_callable来查看
$callback
是什么类型的处理程序。
If $callback
is_callable
, I know enough and use that function/method/closure. 如果
$callback
is_callable
,我知道并使用该函数/方法/闭包。 If it's not callable and it's a string, it's probably the name of a method in $this
class. 如果它不可调用并且它是一个字符串,那么它可能是
$this
类中方法的名称。 It might exist and it might not. 它可能存在,也可能不存在。 If I let it up to PHP, it will 'throw' a nice fatal error (I like those).
如果我让它升级到PHP,它将“抛出”一个很好的致命错误(我喜欢那些)。
But I want to use mix-ins which means I need the magic method __call
. 但我想使用mix-ins ,这意味着我需要魔术方法
__call
。 __call
is very cool, because it (can) contain(s) logic before the actual function call. __call
非常酷,因为它(可以)在实际函数调用之前包含(s)逻辑。 BUT ... what happens if after __call
the called method doesn't exist? 但是 ......如果在
__call
之后调用的方法不存在会发生什么? 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_callable
的用处,因为在实现__call
, 所有内容都将返回true,因为所有内容都有一个回退(是__call
)。
Is there a way to have both: dynamic methods and useful is_callable
? 有没有办法同时拥有:动态方法和有用的
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. 我希望看到的是某种可
__is_callable
魔术方法__is_callable
,当is_callable(array($object, $method))
被调用时,PHP将“咨询”。
On php.net, I can't find anyone who is as bumbed about this as me. 在php.net上,我找不到任何像我这样对此感到愚蠢的人。 This can't be it!
这不可能! If you use
__call
, you can't use is_callable
anymore!? 如果你使用
__call
,你就不is_callable
使用is_callable
了!?
Am I making any sense? 我有意义吗?
PS. 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. 我已经研究过
method_exists
但这还不够好(即使我可以过滤掉所有的闭包和全局函数),因为它将为私有和受保护的方法以及public返回true。
edit 编辑
I made a 'better' is_callable that checks for mix-ins but I think it's pretty expensive. 我制作了一个'更好'的is_callable来检查混音,但我觉得它很贵。
First idea: how about checking if method exists in get_class_methods()
? 第一个想法:如何检查
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. 第二个想法:如果你正在寻找的函数不存在于类中,你也可以检查(使用
method_exists
)如果类包含__call
魔术方法,如果它不明显地调用不存在的函数是非法的。 If class contains __call
method, it's likely that caller knows what he's doing. 如果类包含
__call
方法,则调用者可能知道他正在做什么。
These are just my thoughts, I assume Python to be better in this kind of things. 这些只是我的想法,我认为Python在这类事情上会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.