Extend 2 Classes PHP

2019-07-14 07:26发布

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

标签: php oop
5条回答
不美不萌又怎样
2楼-- · 2019-07-14 07:42

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

查看更多
疯言疯语
3楼-- · 2019-07-14 07:50

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

Try using composition and re-arranging your class structure.

查看更多
何必那么认真
4楼-- · 2019-07-14 07:53

class a

class b extends a

class c extends b

done.

查看更多
疯言疯语
5楼-- · 2019-07-14 07:55

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

查看更多
家丑人穷心不美
6楼-- · 2019-07-14 08:04

Class c extends a implements interface

Done

查看更多
登录 后发表回答