Inheritance: Vertical or Horizontal

95
Inheritance: Vertical or Horizontal? Mark Niebergall https://joind.in/talk/77415

Transcript of Inheritance: Vertical or Horizontal

Page 1: Inheritance: Vertical or Horizontal

Inheritance: Vertical or Horizontal?

Mark Niebergallhttps://joind.in/talk/77415

Page 2: Inheritance: Vertical or Horizontal

About Mark Niebergall• PHP since 2005 • Masters degree in MIS • Senior Software Engineer • Drug screening project • UPHPU President • CSSLP, SSCP Certified and SME • Drones, fishing, skiing, father, husband

Page 3: Inheritance: Vertical or Horizontal

About Mark Niebergall• Twitter @mbniebergall • phpug.slack.com • phpchat.slack.com • phpcommunity.slack.com • utos.slack.com

Page 4: Inheritance: Vertical or Horizontal
Page 5: Inheritance: Vertical or Horizontal

Inheritance: Vertical or Horizontal?

Page 6: Inheritance: Vertical or Horizontal
Page 7: Inheritance: Vertical or Horizontal
Page 8: Inheritance: Vertical or Horizontal

Inheritance: Vertical or Horizontal?

• Objective

- Solid understanding of how inheritance works in PHP

- Know when and how to use inheritance in PHP

- Able to apply material to current projects

Page 9: Inheritance: Vertical or Horizontal

Inheritance: Vertical or Horizontal?

• The Problem

• Vertical inheritance

• Horizontal inheritance

• Applied use

• Considerations

Page 10: Inheritance: Vertical or Horizontal

The Problem

Page 11: Inheritance: Vertical or Horizontal

The Problem• Copy and paste

Page 12: Inheritance: Vertical or Horizontal

The Problem• Procedural code

- Simple

- Efficient

Page 13: Inheritance: Vertical or Horizontal

The Problem• Procedural code

- Lacks organization

- Difficult to scale

Page 14: Inheritance: Vertical or Horizontal

The Problem• Functions

- Reusable

- Some organization

- No grouping

Page 15: Inheritance: Vertical or Horizontal

The Problem• Concrete Class

- Basic code organization with classes

- Concrete class can be instantiated

Page 16: Inheritance: Vertical or Horizontal

The Problem• Concrete Class

- Properties

- Constants

- Methods

- Magic methods

Page 17: Inheritance: Vertical or Horizontal

The Problem• Concrete Class

- class X { const SOME_THING = ‘Thing’; protected $flag = true; private function secretStuff() {…} public function doSomething() {…}}

Page 18: Inheritance: Vertical or Horizontal

The Problem• Concrete Class

- Problems by themselves

‣ Code reuse, duplication

‣ Code enforcement

‣ Code organization

Page 19: Inheritance: Vertical or Horizontal

The Problem• Concrete Class

- Problems by themselves

‣ Classes can become bloated

‣ No separation of services

‣ Complicated code

Page 20: Inheritance: Vertical or Horizontal

The Problem• Concrete Class

- Problems by themselves

‣ Hard to debug

‣ Testability goes down

Page 21: Inheritance: Vertical or Horizontal

The Problem• Concrete Class

- Some problems solved with Dependency Injection (DI)

‣ public function __construct(Dependency $d) { $this->dependency = $d;}public function setThing(Thing $thing) { $this->thing = $thing;}public function __set($key, $value) { if ($value instanceof Thing) {$this->thing = $value;}}

Page 22: Inheritance: Vertical or Horizontal

The Problem• Concrete Class

- Problems solved with inheritance

Page 23: Inheritance: Vertical or Horizontal

The Problem• Concrete Class

- Composition over Inheritance

- Inheritance over Composition

Page 24: Inheritance: Vertical or Horizontal

Vertical Inheritance

Page 25: Inheritance: Vertical or Horizontal

Vertical Inheritance• Family

- Olsen line

‣ Stature

‣ Stubbornness

- Niebergall

‣ Engineering

Page 26: Inheritance: Vertical or Horizontal

Vertical Inheritance• Service provider

- Education

‣ University

- Utilities

‣ Garbage collection

Page 27: Inheritance: Vertical or Horizontal

Vertical Inheritance• Animal

- Pet

‣ Dog

• German Shepard

‣ Cat

• Short-hair

Page 28: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

• Interface

Page 29: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

- X is a Y

Page 30: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

- X is a Y

‣ Mark is a Niebergall

‣ Garbage Collection is a Utility

‣ Cat is a Pet

Page 31: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

- X is a Y

‣ Pet is an Animal

• Cat is a Pet

• Short-hair is a Cat

Page 32: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

- abstract class Animal {…}

‣ abstract class Pet extends Animal {…}

• abstract class Cat extends Pet {…}

• class class ShortHair extends Cat {…}

Page 33: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

- abstract class Animal { abstract public function breathe(Lungs $lungs) : Lungs; private function beAlive() : Animal {…}}abstract class Cat { public function breathe(Lungs $lungs) : Lungs {…} final protected function purr() {…}}

Page 34: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

- class ShortHair { public function takeNap() { $this->goToSleep(); } public function getPetted() { $this->purr(); }}

Page 35: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

- Extend only one class at a time

- Can have multiple levels of inheritance

- Can not instantiate abstract

Page 36: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

- Abstract functions must be implemented

- Signatures must match

Page 37: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

- Benefits

‣ Reduce code duplication

‣ Code organization

‣ Logical flow

Page 38: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

- Push code up the vertical inheritance chain as far as it logically makes sense

Page 39: Inheritance: Vertical or Horizontal

Vertical Inheritance• Abstract

- Questions?

Page 40: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

Page 41: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- interface X { const SOME_VALUE = 123; public function processThing(Thing $thing) : Thing;}

Page 42: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- Object interface

Page 43: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- Connect two things together

‣ User interface

‣ Driver and vehicle

‣ Controller

Page 44: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- Contract defining what needs to happen but not how

- Contract between concept and implementation

Page 45: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- Define interaction requirements

- Leaves implementation to be handled in classes

- Can not be instantiated

Page 46: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- Class can implement multiple interfaces

- Can be used with abstract and class

- Can not be used with a trait

‣ RFC in discussion about that

Page 47: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- interface X {…}

- class A implements X {…}

- abstract class Z implements V, W, X {…}

Page 48: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- Interface method signatures are defined

- No method content

- Method signature must match when implemented

- Methods must be implemented

- Interface can extend one or many interfaces

Page 49: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- All methods must be public

- Properties are not allowed

- Can have public constants

Page 50: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- Light from different sources

- interface LightInterface { const LIGHT_SPEED = 299792458; public function shine(Energy $energy) : Light; public function travel(float $x, float $y, float $z);}

Page 51: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- Laptop computer

‣ USB

‣ Video and audio output

‣ Card reader

Page 52: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- interface Power { public function receivePower(Volt $v, Current $i);}interface Usb extends Power{ public function transferData(Data $data);}interface Hdmi { public function transmitVideo(Video $video);}abstract class Laptop implements Usb, Hdmi {…}

Page 53: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- Underwater pets

- Auditing

- Authentication methods

- Request and response

‣ Middleware

Page 54: Inheritance: Vertical or Horizontal

Vertical Inheritance• Interface

- Questions?

Page 55: Inheritance: Vertical or Horizontal

Vertical Inheritance

Page 56: Inheritance: Vertical or Horizontal

Horizontal Inheritance

Page 57: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Personal characteristics

- Hair color

- Personality

Page 58: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Underwater creature

- Fish

- Mammal

- Reptile

Page 59: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

Page 60: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- trait M { protected $thing; public function doThing(Thing $thing) : Thing {…}}class X { use M; protected function something(Thing $thing) : Thing { return $this->doThing($this->thing($thing); }}

Page 61: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Promotes code reuse

- Unrestricted by inheritance hierarchies

- Available since PHP 5.4.0

Page 62: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Pull in grouped functionality

- Multiple inheritance horizontally

- Similar to a concrete class but cannot be instantiated

Page 63: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- “Aware” of a concept

‣ ContainerAware

‣ AuditAware

‣ AdapterAware

Page 64: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Can be used alongside abstracts and interfaces in a class

- Class can use multiple traits

- Used in abstract class or concrete class

- Can have properties and methods

Page 65: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Can not be instantiated

- Can not implement an interface

- Can not extend a class

Page 66: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Methods and properties in trait(s) become available to class

- Traits can not have class constants

Page 67: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Order of precedence

‣ Class and trait method naming collisions

• Current class

• Trait

• Parent class

Page 68: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- abstract class TheBase { public function hello() { echo ‘Hello from the base.’; }}trait TheTrait { public function hello() { echo ‘Hello from the trait.’; }}class TheClass extends TheBase { use TheTrait; public function hello() { echo ‘Hello from the class.’; }}

Page 69: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- trait TheTrait { public function hello() { echo ‘Hello from the trait.’; }}class TheClass extends TheBase { use TheTrait { TheTrait::hello as helloTrait; }; public function hello() { echo ‘Hello from the class.’; } public function helloBase() { parent::hello(); }}

Page 70: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Naming conflicts must be resolved

‣ Fatal error if not

Page 71: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- ‘insteadof’ to use just one

- ‘as’ to alias and allow using more than one

Page 72: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- class Thing { use TraitA, TraitB { TraitA::doThing insteadof TraitB; TraitA::doOtherThing as doOtherThingA; TraitB::doOtherThing as doOtherThingB; }}$thing->doThing();$thing->doOtherThingA();$thing->doOtherThingB();

Page 73: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Can change visibility modifier

‣ use Trait { methodName as protected; }

‣ class Thing { use Trait { Trait::doThing as public; }}$thing->doThing();

Page 74: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Traits can have properties

- Class properties must match visibility and initial value if same name

‣ trait X { public $something = ‘something’;}class Z { use X; public $something = ‘something’;}

Page 75: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Traits can use other traits

‣ trait Thing { use ThingA, ThingB;}

Page 76: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Traits can have abstract methods

‣ trait Thing { abstract protected function doSomething();}class X { use Thing; protected function doSomething() { echo ‘doing something’; }}

Page 77: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Traits can have static methods

‣ trait Thing { public static function anotherThing();}class X { use Thing; }X::anotherThing();

Page 78: Inheritance: Vertical or Horizontal

Horizontal Inheritance• Trait

- Recap

‣ Overcome multiple inheritance problem

‣ Horizontal inheritance

Page 79: Inheritance: Vertical or Horizontal

Horizontal Inheritance

Page 80: Inheritance: Vertical or Horizontal

Applied Use

Page 81: Inheritance: Vertical or Horizontal

Applied Use• Vertical or Horizontal inheritance?

- Vertical: abstract, interface

- Horizontal: trait

Page 82: Inheritance: Vertical or Horizontal

Applied Use• Vertical or Horizontal inheritance?

- Vertical is heavily used

- Most concrete classes extend an abstract in projects

Page 83: Inheritance: Vertical or Horizontal

Applied Use• Vertical or Horizontal inheritance?

- My experience

‣ Most use vertical

‣ Few use horizontal

Page 84: Inheritance: Vertical or Horizontal

Applied Use• Vertical or Horizontal inheritance?

- My experience with concrete classes

‣ Most extend an abstract

‣ Some implement an interface

‣ Few use trait

Page 85: Inheritance: Vertical or Horizontal

Considerations

Page 86: Inheritance: Vertical or Horizontal

Considerations• Open discussion

Page 87: Inheritance: Vertical or Horizontal

Considerations• Why need for abstracts, interfaces, and traits

Page 88: Inheritance: Vertical or Horizontal

Considerations• Benefits of using combinations of abstracts, interfaces, and

traits

Page 89: Inheritance: Vertical or Horizontal

Considerations• Traits vs Dependency Injection

Page 90: Inheritance: Vertical or Horizontal

Considerations• Role of Dependency Injection

Page 91: Inheritance: Vertical or Horizontal

Considerations• Code complexity

- How far to take abstraction

- Readability

- Code consolidation

Page 92: Inheritance: Vertical or Horizontal

Considerations• Code testability

- Unit tests

- Testing abstract instead of concrete implementation

Page 93: Inheritance: Vertical or Horizontal

Considerations• Evaluating current projects

- Static code analysis

- Leverage inheritance

Page 94: Inheritance: Vertical or Horizontal

Questions?• Please rate this talk

- https://joind.in/talk/77415

Page 95: Inheritance: Vertical or Horizontal

Sources• https://en.wikipedia.org/wiki/Convair_B-36_Peacemaker

• http://www.boeing.com/resources/boeingdotcom/commercial/737ng/assets/images/marquee.jpg

• https://www.gannett-cdn.com

• http://pop.h-cdn.co/assets/15/20/1600x800/landscape-1431629947-marty-delorean.jpg

• http://www.sbs.com.au/movies/sites/sbs.com.au.film/files/styles/full/public/Dynamite_704.jpg?itok=_XPK3CbV&mtime=1404453609

• http://latimesblogs.latimes.com/.a/6a00d8341c630a53ef01310f8dd9fe970c-pi

• https://i.pinimg.com/474x/f8/32/b4/f832b4c3b92cc4ccd19a285f68a4197a--merlin-olsen-little-houses.jpg

• php.net