PHP中的多重继承 [英] Multiple Inheritance in PHP

查看:129
本文介绍了PHP中的多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种好的,干净的方式来解决PHP5仍然不支持多重继承的事实。这是类层次结构:

I'm looking for a good, clean way to go around the fact that PHP5 still doesn't support multiple inheritance. Here's the class hierarchy:

消息

- TextMessage

-------- InvitationTextMessage

- EmailMessage

-------- InvitationEmailMessage

Message
-- TextMessage
-------- InvitationTextMessage
-- EmailMessage
-------- InvitationEmailMessage

两种类型的Invitation *类有很多共同之处;我希望有一个共同的家长班,邀请,他们都会继承。不幸的是,它们与当前的祖先有很多共同点...... TextMessage和EmailMessage。这里有多重继承的经典愿望。

The two types of Invitation* classes have a lot in common; i'd love to have a common parent class, Invitation, that they both would inherit from. Unfortunately, they also have a lot in common with their current ancestors... TextMessage and EmailMessage. Classical desire for multiple inheritance here.

解决这个问题的最轻量级方法是什么?

What's the most light-weight approach to solve the issue?

谢谢!

推荐答案

Alex,大多数时候你需要多重继承是一种信号,你的对象结构有些不正确。在你概述的情况下,我发现你的课堂责任太宽泛了。如果Message是应用程序业务模型的一部分,则不应该关注呈现输出。相反,您可以拆分责任并使用MessageDispatcher发送使用text或html后端传递的Message。我不知道你的代码,但让我用这种方式模拟它:

Alex, most of the times you need multiple inheritance is a signal your object structure is somewhat incorrect. In situation you outlined I see you have class responsibility simply too broad. If Message is part of application business model, it should not take care about rendering output. Instead, you could split responsibility and use MessageDispatcher that sends the Message passed using text or html backend. I don't know your code, but let me simulate it this way:

$m = new Message();
$m->type = 'text/html';
$m->from = 'John Doe <jdoe@yahoo.com>';
$m->to = 'Random Hacker <rh@gmail.com>';
$m->subject = 'Invitation email';
$m->importBody('invitation.html');

$d = new MessageDispatcher();
$d->dispatch($m);

这样你可以为Message类添加一些特化:

This way you can add some specialisation to Message class:

$htmlIM = new InvitationHTMLMessage(); // html type, subject and body configuration in constructor
$textIM = new InvitationTextMessage(); // text type, subject and body configuration in constructor

$d = new MessageDispatcher();
$d->dispatch($htmlIM);
$d->dispatch($textIM);

请注意,MessageDispatcher会根据<$ c $决定是以HTML格式还是纯文本格式发送c>在传递的Message对象中输入属性。

Note that MessageDispatcher would make a decision whether to send as HTML or plain text depending on type property in Message object passed.

// in MessageDispatcher class
public function dispatch(Message $m) {
    if ($m->type == 'text/plain') {
        $this->sendAsText($m);
    } elseif ($m->type == 'text/html') {
        $this->sendAsHTML($m);
    } else {
        throw new Exception("MIME type {$m->type} not supported");
    }
}

总结一下,责任分为两个班级。消息配置在InvitationHTMLMessage / InvitationTextMessage类中完成,发送算法委托给调度程序。这称为策略模式,您可以在此处上阅读更多信息。

To sum it up, responsibility is split between two classes. Message configuration is done in InvitationHTMLMessage/InvitationTextMessage class, and sending algorithm is delegated to dispatcher. This is called Strategy Pattern, you can read more on it here.

这篇关于PHP中的多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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