如何检查一个类是否在 PHP 中使用了 trait?

编程入门 行业动态 更新时间:2024-10-28 02:22:23
本文介绍了如何检查一个类是否在 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?

本文发布于:2023-05-29 21:35:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/353354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使用了   PHP   trait

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!