请注意,一个 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?
发布评论