Extend 2 Classes PHP

2019-07-14 07:37发布

问题:

I have 3 classes (3 files):

a.class.php
b.class.php
c.class.php

I want to extend class a and b in the class c (file 3):

How I could do that? I want to use both class functions of a + b in my new class C

回答1:

There is no multiple inheritance in PHP. So you can't do that.

Try using composition and re-arranging your class structure.



回答2:

You are asking for multiple inheritance, which is not supported by php. You should have a look at composition instead.



回答3:

There is a way of achieving this in PHP using mixins. See this http://www.phpdeveloper.org/news/6139 for example. However I would probably try to find a different way of designing your code so you don't have to use it.

Otherwise PHP 5.4 will bring Traits which will natively support what you want: http://simas.posterous.com/new-to-php-54-traits



回答4:

class a

class b extends a

class c extends b

done.



回答5:

Class c extends a implements interface

Done



标签: php oop