如何扩展多个实用程序类 [英] How to extend multiple utility classes

查看:108
本文介绍了如何扩展多个实用程序类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题有点针对PHP,但它可能也适用于其他语言.在PHP中,您只能扩展一个类.但是,如果您需要更多的课程怎么办?

This question is kinda aimed towards PHP but it probably applies to other languages aswell. In PHP you can only extend one class. But what if you need more classes?

假设我有以下课程:

class MyClass extends Observer, Logger, OtherUtilClass

MyClass不能扩展多个类.但是它需要成为观察者.它可能还需要其他一些基类才能完全起作用.最好的方法是什么?

MyClass can't extend more than one class. But it needs to be an observer. And it might need some other base class aswell to fully function. What would be the best approach for this?

推荐答案

您的想法导致 multiple inheritance 及其它暂时无法在PHP中使用.

No

Your idea leads to multiple inheritance and it is not available in PHP for good.

非常建议您阅读这个问题以了解原因你不应该尝试这个.

I extremely recommended you read this question to see why you shouldn't try this.

但是,您仍然可以链接类.如果以正确的方式完成操作,则可以获得所有类的功能.

However, you still can chain the classes. If done in a proper way you can get the functionality of all classes.

Class Observer {}
Class Logger extends Observer {}
Class OtherUtilClasses extends Logger {}

//Now
class MyClass extends OtherUtilClasses {}

这篇关于如何扩展多个实用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆