PHP __call与method_exists [英] PHP __call vs method_exists

查看:83
本文介绍了PHP __call与method_exists的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的项目包含类似于call_user_func(_array)的包装程序,该程序在执行前会进行一些检查. 这些检查之一是method_exists(如果提供的第一个参数是类的实例,第二个是方法名称) 另一个is_callable. 如果其中一项检查失败,则该函数将引发异常.

The Project I'm working on contains something like a wrapper for call_user_func(_array) which does some checks before execution. One of those checks is method_exists (In Case the supplied first argument is an instance of a class and the second is a method name) The other is_callable. The function will throw an exception if one of those checks fails.

我的代码包含一个具有函数名称(setFoo,setBar等)的数组以及用于重载(__call)的php magic函数,该函数处理某些变量(更好的某些数组元素)的设置,替换和删除.

My Code contains an array with function names (setFoo, setBar, etc.) and the php magic function for overloading (__call) which handles setting, replacing and deletion of certain variables (better certain array elements).

问题: 如果未定义函数,method_exists将返回false.

The Problem: method_exists will return false if the function is not defined.

如果__call函数正确处理了请求,我是否有机会得到真假?

Do I have any chance to get a true if the __call function does proper handling of the request?

推荐答案

__call处理对不存在的方法的调用. method_exists是检查方法是否存在的自省方法.

__call handles calls to methods that don't exist. method_exists is an introspection method that checks the existence of a method.

如何确定__call处理方法?我认为,如果无法处理您的请求,则必须在__call中手动引发异常,并在本应使用method_exists的代码中捕获该异常.为此目的存在 BadMethodCallException .

How can __call be determined to handle a method? I think you have to throw an exception manually in __call if doesn't handle your request and catch the exception in the code that would otherwise use method_exists. BadMethodCallException exists for this purpose.

这篇关于PHP __call与method_exists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆