如何在没有反射的情况下检查方法或属性的可见性?
我正在写一个简单的 php 框架。现在我需要一种更好的方法来检查方法或属性是私有的还是公共的。目前我正在使用 Reflection 类,但几乎每个属性访问都需要它。
我需要它来实现神奇的 __get
,如果其中有公共方法 getTestProperty()
,它允许您调用 $object->testProperty
。
我正在寻找另一种方法来做到这一点(因为性能)。有没有更快的可能?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
is_callable
来检查是否可以在对象上调用该方法:如果需要检查某些属性是否存在,请使用
property_exists
:最后还有
method_exists
:You can use
is_callable
to check if the method can be called on the object:If you need to check for some property existence use
property_exists
:And finaly there are
method_exists
:还有2个有用的功能:
bool method_exists(class_name_or_object,method_name) - 如果方法存在(公共甚至受保护和私有)或 false,则返回 true
array get_class_methods(class_name_or_object) - 返回类或对象的公共方法的数组
There is also 2 useful functions:
bool method_exists(class_name_or_object,method_name) - will return true if method exists (public and even protected and private) either false
array get_class_methods(class_name_or_object) - return array of public methods of class or object