有什么办法可以在php中实现多重继承吗? [英] Is there any way to achieve multiple inheritance in php?

查看:82
本文介绍了有什么办法可以在php中实现多重继承吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有一个家长班

class parent { }

.....

此父级具有三个子类

class child1 { }

class child2 { }

class child3 { }

这些子类还有更小的部分,例如

and these child classes have further smaller parts like

class child1subpar1 { }

class child1subpar2 {

       public function foo() {
          echo "hi";
       }

}

class child2subpar1 { }

class child2subpar2 { }

现在,如何像这样总结整个

Now, how to sum this whole up like

class child1 extends child1subpar1, child1subpar2 { }
class child2 extends child2subpar1, childsubpar1 { }

class parent extends child1,child2,child3 { }

我需要在其继承的类中执行方法,并执行类似的操作

I need to execute the methods in its inherited classes and do something like this

$ objparent =新父母; $ objparent-> foo();

$objparent = new parent; $objparent -> foo();

推荐答案

否,但是通常认为多重继承是一种不好的做法.您应该改为使用组合,因此您只需在类中使用要继承的类的实例即可.

No, but multiple inheritance is generally considered a bad practice. You should favor composition instead, so you just use instances of classes you wanted to inherit inside your class.

现在,当我再次调查您的问题时,它甚至不是继承问题,您应该使用组合.也许如果您提供了您希望该班级做什么的更多细节,我们应该回答得更准确.

And now when I look into your question again, it's not even an inheritance issue, you should use composition. Maybe if you provided more detail what you expect that class to do, we should answer more accurately.

更新:

您将需要为每个要使用的类的方法创建一个方法-称为Facade设计模式.也许您不知道可以像这样调用内部对象的方法:

You will need to create one method for each of these classes' method which you would want to use - it's called Facade design pattern. Or maybe you are not aware that you can call methods of inner objects like this:

$parent->subObject->subSubObject->wantedMethod();

门面图案:

http://en.wikipedia.org/wiki/Facade_pattern

除了创建一个类并定义要使用的每个方法外,在该方法内部调用任何内部实例的任何方法都不过是什么.但我真的没有看到任何好处,而不是打电话给 实例和方法分层

in your case facade wouldn't be anything else than creating one class and define every single method you want to use, and inside that method calling any method of any of your inner instances. But i really don't see any benefit coming from this instead of calling instances and methods hierarchically

这篇关于有什么办法可以在php中实现多重继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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