Inheritance: Vertical or Horizontal
-
Upload
mark-niebergall -
Category
Software
-
view
142 -
download
0
Transcript of Inheritance: Vertical or Horizontal
![Page 1: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/1.jpg)
Inheritance: Vertical or Horizontal?
Mark Niebergallhttps://joind.in/talk/77415
![Page 2: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/3.jpg)
About Mark Niebergall• Twitter @mbniebergall • phpug.slack.com • phpchat.slack.com • phpcommunity.slack.com • utos.slack.com
![Page 4: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/4.jpg)
![Page 5: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/5.jpg)
Inheritance: Vertical or Horizontal?
![Page 6: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/6.jpg)
![Page 7: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/7.jpg)
![Page 8: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/9.jpg)
Inheritance: Vertical or Horizontal?
• The Problem
• Vertical inheritance
• Horizontal inheritance
• Applied use
• Considerations
![Page 10: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/10.jpg)
The Problem
![Page 11: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/11.jpg)
The Problem• Copy and paste
![Page 12: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/12.jpg)
The Problem• Procedural code
- Simple
- Efficient
![Page 13: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/13.jpg)
The Problem• Procedural code
- Lacks organization
- Difficult to scale
![Page 14: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/14.jpg)
The Problem• Functions
- Reusable
- Some organization
- No grouping
![Page 15: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/15.jpg)
The Problem• Concrete Class
- Basic code organization with classes
- Concrete class can be instantiated
![Page 16: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/16.jpg)
The Problem• Concrete Class
- Properties
- Constants
- Methods
- Magic methods
![Page 17: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/18.jpg)
The Problem• Concrete Class
- Problems by themselves
‣ Code reuse, duplication
‣ Code enforcement
‣ Code organization
![Page 19: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/19.jpg)
The Problem• Concrete Class
- Problems by themselves
‣ Classes can become bloated
‣ No separation of services
‣ Complicated code
![Page 20: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/20.jpg)
The Problem• Concrete Class
- Problems by themselves
‣ Hard to debug
‣ Testability goes down
![Page 21: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/22.jpg)
The Problem• Concrete Class
- Problems solved with inheritance
![Page 23: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/23.jpg)
The Problem• Concrete Class
- Composition over Inheritance
- Inheritance over Composition
![Page 24: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/24.jpg)
Vertical Inheritance
![Page 25: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/25.jpg)
Vertical Inheritance• Family
- Olsen line
‣ Stature
‣ Stubbornness
- Niebergall
‣ Engineering
![Page 26: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/26.jpg)
Vertical Inheritance• Service provider
- Education
‣ University
- Utilities
‣ Garbage collection
![Page 27: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/27.jpg)
Vertical Inheritance• Animal
- Pet
‣ Dog
• German Shepard
‣ Cat
• Short-hair
![Page 28: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/28.jpg)
Vertical Inheritance• Abstract
• Interface
![Page 29: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/29.jpg)
Vertical Inheritance• Abstract
- X is a Y
![Page 30: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/30.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/31.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/32.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/34.jpg)
Vertical Inheritance• Abstract
- class ShortHair { public function takeNap() { $this->goToSleep(); } public function getPetted() { $this->purr(); }}
![Page 35: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/35.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/36.jpg)
Vertical Inheritance• Abstract
- Abstract functions must be implemented
- Signatures must match
![Page 37: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/37.jpg)
Vertical Inheritance• Abstract
- Benefits
‣ Reduce code duplication
‣ Code organization
‣ Logical flow
![Page 38: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/38.jpg)
Vertical Inheritance• Abstract
- Push code up the vertical inheritance chain as far as it logically makes sense
![Page 39: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/39.jpg)
Vertical Inheritance• Abstract
- Questions?
![Page 40: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/40.jpg)
Vertical Inheritance• Interface
![Page 41: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/41.jpg)
Vertical Inheritance• Interface
- interface X { const SOME_VALUE = 123; public function processThing(Thing $thing) : Thing;}
![Page 42: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/42.jpg)
Vertical Inheritance• Interface
- Object interface
![Page 43: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/43.jpg)
Vertical Inheritance• Interface
- Connect two things together
‣ User interface
‣ Driver and vehicle
‣ Controller
![Page 44: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/44.jpg)
Vertical Inheritance• Interface
- Contract defining what needs to happen but not how
- Contract between concept and implementation
![Page 45: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/45.jpg)
Vertical Inheritance• Interface
- Define interaction requirements
- Leaves implementation to be handled in classes
- Can not be instantiated
![Page 46: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/46.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/47.jpg)
Vertical Inheritance• Interface
- interface X {…}
- class A implements X {…}
- abstract class Z implements V, W, X {…}
![Page 48: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/48.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/49.jpg)
Vertical Inheritance• Interface
- All methods must be public
- Properties are not allowed
- Can have public constants
![Page 50: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/50.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/51.jpg)
Vertical Inheritance• Interface
- Laptop computer
‣ USB
‣ Video and audio output
‣ Card reader
![Page 52: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/52.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/53.jpg)
Vertical Inheritance• Interface
- Underwater pets
- Auditing
- Authentication methods
- Request and response
‣ Middleware
![Page 54: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/54.jpg)
Vertical Inheritance• Interface
- Questions?
![Page 55: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/55.jpg)
Vertical Inheritance
![Page 56: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/56.jpg)
Horizontal Inheritance
![Page 57: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/57.jpg)
Horizontal Inheritance• Personal characteristics
- Hair color
- Personality
![Page 58: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/58.jpg)
Horizontal Inheritance• Underwater creature
- Fish
- Mammal
- Reptile
![Page 59: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/59.jpg)
Horizontal Inheritance• Trait
![Page 60: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/60.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/61.jpg)
Horizontal Inheritance• Trait
- Promotes code reuse
- Unrestricted by inheritance hierarchies
- Available since PHP 5.4.0
![Page 62: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/62.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/63.jpg)
Horizontal Inheritance• Trait
- “Aware” of a concept
‣ ContainerAware
‣ AuditAware
‣ AdapterAware
![Page 64: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/64.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/65.jpg)
Horizontal Inheritance• Trait
- Can not be instantiated
- Can not implement an interface
- Can not extend a class
![Page 66: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/66.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/67.jpg)
Horizontal Inheritance• Trait
- Order of precedence
‣ Class and trait method naming collisions
• Current class
• Trait
• Parent class
![Page 68: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/68.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/69.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/70.jpg)
Horizontal Inheritance• Trait
- Naming conflicts must be resolved
‣ Fatal error if not
![Page 71: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/71.jpg)
Horizontal Inheritance• Trait
- ‘insteadof’ to use just one
- ‘as’ to alias and allow using more than one
![Page 72: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/72.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/73.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/74.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/75.jpg)
Horizontal Inheritance• Trait
- Traits can use other traits
‣ trait Thing { use ThingA, ThingB;}
![Page 76: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/76.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/77.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/78.jpg)
Horizontal Inheritance• Trait
- Recap
‣ Overcome multiple inheritance problem
‣ Horizontal inheritance
![Page 79: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/79.jpg)
Horizontal Inheritance
![Page 80: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/80.jpg)
Applied Use
![Page 81: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/81.jpg)
Applied Use• Vertical or Horizontal inheritance?
- Vertical: abstract, interface
- Horizontal: trait
![Page 82: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/82.jpg)
Applied Use• Vertical or Horizontal inheritance?
- Vertical is heavily used
- Most concrete classes extend an abstract in projects
![Page 83: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/83.jpg)
Applied Use• Vertical or Horizontal inheritance?
- My experience
‣ Most use vertical
‣ Few use horizontal
![Page 84: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/84.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/85.jpg)
Considerations
![Page 86: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/86.jpg)
Considerations• Open discussion
![Page 87: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/87.jpg)
Considerations• Why need for abstracts, interfaces, and traits
![Page 88: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/88.jpg)
Considerations• Benefits of using combinations of abstracts, interfaces, and
traits
![Page 89: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/89.jpg)
Considerations• Traits vs Dependency Injection
![Page 90: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/90.jpg)
Considerations• Role of Dependency Injection
![Page 91: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/91.jpg)
Considerations• Code complexity
- How far to take abstraction
- Readability
- Code consolidation
![Page 92: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/92.jpg)
Considerations• Code testability
- Unit tests
- Testing abstract instead of concrete implementation
![Page 93: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/93.jpg)
Considerations• Evaluating current projects
- Static code analysis
- Leverage inheritance
![Page 95: Inheritance: Vertical or Horizontal](https://reader034.fdocuments.us/reader034/viewer/2022042619/5a6512577f8b9af5298b4953/html5/thumbnails/95.jpg)
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