如何检查一个类是否在 PHP 中使用了 trait? [英] How to check if a class uses a trait in PHP?

查看:75
本文介绍了如何检查一个类是否在 PHP 中使用了 trait?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,一个 trait 可能会使用其他 trait,因此该类可能不会直接使用该 trait.并且该类也可以从使用 trait 的父类继承.

Notice that a trait may use other traits, so the class may not be using that trait directly. And also the class may be inherited from a parent class who is the one uses the trait.

这是一个可以在几行内解决的问题还是我必须做一些循环?

Is this a question that can be solved within several lines or I would have to do some loops?

推荐答案

class_uses() 函数将返回一个包含该类使用的所有特征名称的数组,并将通过向它传递类名或实例来工作......但是,您需要通过继承树以获取使用的所有特征,并通过每个特征

The class_uses() function will return an array containing the names of all traits used by that class, and will work by passing it a class name or an instance.... however, you'd need to "recurse" through the inheritance tree to get all traits used, and through each trait as well

编辑

请注意,stealz at op dot pl 提供了一个示例函数,展示了如何在链接的 PHP 文档页面的注释部分执行此递归

Note that stealz at op dot pl has provided an example function showing how to do this recursion in the comments section of the linked PHP Docs page

这篇关于如何检查一个类是否在 PHP 中使用了 trait?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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