2

With "implements" I can force a class to define certain functions, but I need to add some functions to the interface to avoid duplicate code. This is only possible if I make the class "abstract" and use "extends". But "extends" only accepts one class. Because each of these classes are different, sort of like features of an app, I can't just extend one with the other. So is there any way to have

class My extends Feature1, Feature2, Feature3{
}

??

xpedobearx
  • 707
  • 3
  • 8
  • 13
  • 1
    You can use `Traits`if your php version is higher than 5.4 – Med Apr 14 '15 at 09:23
  • To extend on what @Med said: Traits might be exactly the right solution to your particular problem but just note that unlike with classes and interfaces you cannot use [type hinting](http://php.net/manual/en/language.oop5.typehinting.php) with them. – tmt Apr 14 '15 at 09:33

1 Answers1

5

Php doesnot allow multiple inhritance. There are two ways to do this -

First

class a { }

class b extends a { }

class c extends b { }

Interfaces -

interface sb { }

class c extends a implements sb { }
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • 2
    As of PHP 5.4 or later you can use Traits to achieve (kind of) multi-inheritance: http://php.net/manual/language.oop5.traits.php – Gerald Jun 09 '18 at 23:16