用php 用拼出一个菱形,如何解决PHP中的菱形问题?

I have searched for solutions to the diamond problem but the only one I have found is using traits which I can't use in my case, so I'm asking here to see if anyone has an alternative solution.

I have a base class Controller (I cannot change this class) and have two subclasses SecurityController and DevController. Each of these subclasses introduces methods which also use methods inside the base class. I then have a final class ApplicationController which, ideally, would extend both SecurityController and DevController. Of course, this isn't possible in PHP (single inheritance only).

So my question becomes - what would be the best way to resolve this? I came across traits but then realised that doesn't work as the 2 subclasses (which I thought might suit traits) both need to extend Controller to access methods inside it. The only other alternative I can see is forcing SecurityController to extend DevController (or vice versa). Although this works it is not ideal as these two classes come from separate modules which I was hoping to create as a drop in and use as is type of plugin.

This post on code review looks promising. An alternative would be good though - I feel I'm likely to introduce errors when trying to improve upon that code.

Note about the solution

The accepted answer is still the best way I have found to resolve this issue in general. However, in this case I was able to use traits. I had a method in SecurityController called beforeExecute($dispatcher) - by changing it to beforeExecuteTrait($controller, $dispatcher) and making SecurityController a trait then having ApplicationController extend Controller, use SecurityController and adding a method in ApplicationController as

public function beforeExecute($dispatcher)

{

return $this->beforeExecuteTrait($this, $dispatcher);

}

and by applying the same logic to DevController I achieved the desired behaviour.

解决方案

It sounds like you could benefit from dependency injection. In other words, you would instantiate the other classes and then inject those instances into your main class for you to use. This avoids any messes with inheritance in PHP.

class A extends B {

}

class C {

/** @var \A */

protected $classa;

public function __construct(\A $class) {

$this->classa = $class;

}

}

$a = new \A();

$c = new \C($a);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值