是否可以动态地扩展类? [英] Is it possible to extend a class dynamically?
问题描述
我有一个类,我需要使用来扩展不同的类(达到数百),取决于标准。在PHP中有一种方法可以通过动态类名来扩展类。
I have a class which I need to use to extend different classes (up to hundreds) depending on criteria. Is there a way in PHP to extend a class by a dynamic class name?
我假设需要一个方法来指定扩展实例化。
I assume it would require a method to specify extension with instantiation.
想法?
推荐答案
我不认为可以动态扩展类如果我错了我很想看看它是怎么做的)。您是否考虑过使用复合模式( http://en.wikipedia.org/wiki/Composite_pattern, http://devzone.zend.com/article/7 )?你可以动态地组合另一个类(甚至多个类 - 这通常用作多重继承的工作)来将你的父类的方法/属性注入到子类中。
I don't think it's possible to dynamically extend a class (however if I'm wrong I'd love to see how it's done). Have you thought about using the Composite pattern (http://en.wikipedia.org/wiki/Composite_pattern, http://devzone.zend.com/article/7)? You could dynamically composite another class (even multiple classes - this is often used as a work around to multiple inheritance) to 'inject' the methods/properties of your parent class into the child class.
这篇关于是否可以动态地扩展类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!