Is it possible to extend a class dynamically?

前端 未结 8 1335
耶瑟儿~
耶瑟儿~ 2020-12-05 10:09

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?

8条回答
  •  渐次进展
    2020-12-05 10:56

    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.

提交回复
热议问题