PHP instanceof for traits

前端 未结 4 954
猫巷女王i
猫巷女王i 2020-12-28 13:37

What is the proper way to check if a class uses a certain trait?

4条回答
  •  清酒与你
    2020-12-28 14:33

    It's not really clean and may not be the right solution for your case. But an alternative is to check if the object or class implements a method of the Trait (as usually you don't overwrite existing methods with Trait)

    if (method_exists($my_object, 'MyTraitSpecificMethod')){
        ...
    }
    

提交回复
热议问题