SlideShare a Scribd company logo
Inheritance: Vertical
or Horizontal?
Mark Niebergall

https://joind.in/talk/77415
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
About Mark Niebergall
• Twitter @mbniebergall
• phpug.slack.com
• phpchat.slack.com
• phpcommunity.slack.com
• utos.slack.com
Inheritance: Vertical or Horizontal
Inheritance: Vertical or
Horizontal?
Inheritance: Vertical or Horizontal
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
Inheritance: Vertical or
Horizontal?
• The Problem

• Vertical inheritance

• Horizontal inheritance

• Applied use

• Considerations
The Problem
The Problem
• Copy and paste
The Problem
• Procedural code

- Simple

- Efficient
The Problem
• Procedural code

- Lacks organization

- Difficult to scale
The Problem
• Functions

- Reusable

- Some organization

- No grouping
The Problem
• Concrete Class

- Basic code organization with classes

- Concrete class can be instantiated
The Problem
• Concrete Class

- Properties

- Constants

- Methods

- Magic methods
The Problem
• Concrete Class

- class X {

const SOME_THING = ‘Thing’;

protected $flag = true;

private function secretStuff() {…}

public function doSomething() {…}

}
The Problem
• Concrete Class

- Problems by themselves

‣ Code reuse, duplication

‣ Code enforcement

‣ Code organization
The Problem
• Concrete Class

- Problems by themselves

‣ Classes can become bloated

‣ No separation of services

‣ Complicated code
The Problem
• Concrete Class

- Problems by themselves

‣ Hard to debug

‣ Testability goes down
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;}

}
The Problem
• Concrete Class

- Problems solved with inheritance
The Problem
• Concrete Class

- Composition over Inheritance

- Inheritance over Composition
Vertical Inheritance
Vertical Inheritance
• Family

- Olsen line

‣ Stature

‣ Stubbornness

- Niebergall

‣ Engineering
Vertical Inheritance
• Service provider

- Education

‣ University

- Utilities

‣ Garbage collection
Vertical Inheritance
• Animal

- Pet

‣ Dog

• German Shepard

‣ Cat

• Short-hair
Vertical Inheritance
• Abstract

• Interface
Vertical Inheritance
• Abstract

- X is a Y
Vertical Inheritance
• Abstract

- X is a Y

‣ Mark is a Niebergall

‣ Garbage Collection is a Utility

‣ Cat is a Pet
Vertical Inheritance
• Abstract

- X is a Y

‣ Pet is an Animal

• Cat is a Pet

• Short-hair is a Cat
Vertical Inheritance
• Abstract

- abstract class Animal {…}

‣ abstract class Pet extends Animal {…}

• abstract class Cat extends Pet {…}

• class class ShortHair extends Cat {…}
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() {…}

}
Vertical Inheritance
• Abstract

- class ShortHair {

public function takeNap() {

$this->goToSleep();

}

public function getPetted() {

$this->purr();

}

}
Vertical Inheritance
• Abstract

- Extend only one class at a time

- Can have multiple levels of inheritance

- Can not instantiate abstract
Vertical Inheritance
• Abstract

- Abstract functions must be implemented

- Signatures must match
Vertical Inheritance
• Abstract

- Benefits

‣ Reduce code duplication

‣ Code organization

‣ Logical flow
Vertical Inheritance
• Abstract

- Push code up the vertical inheritance chain as far as it
logically makes sense
Vertical Inheritance
• Abstract

- Questions?
Vertical Inheritance
• Interface
Vertical Inheritance
• Interface

- interface X {

const SOME_VALUE = 123;

public function processThing(Thing $thing) : Thing;

}
Vertical Inheritance
• Interface

- Object interface
Vertical Inheritance
• Interface

- Connect two things together

‣ User interface

‣ Driver and vehicle

‣ Controller
Vertical Inheritance
• Interface

- Contract defining what needs to happen but not how

- Contract between concept and implementation
Vertical Inheritance
• Interface

- Define interaction requirements

- Leaves implementation to be handled in classes

- Can not be instantiated
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
Vertical Inheritance
• Interface

- interface X {…}

- class A implements X {…}

- abstract class Z implements V, W, X {…}
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
Vertical Inheritance
• Interface

- All methods must be public

- Properties are not allowed

- Can have public constants
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);

}
Vertical Inheritance
• Interface

- Laptop computer

‣ USB

‣ Video and audio output

‣ Card reader
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 {…}
Vertical Inheritance
• Interface

- Underwater pets

- Auditing

- Authentication methods

- Request and response

‣ Middleware
Vertical Inheritance
• Interface

- Questions?
Vertical Inheritance
Horizontal Inheritance
Horizontal Inheritance
• Personal characteristics

- Hair color

- Personality
Horizontal Inheritance
• Underwater creature

- Fish

- Mammal

- Reptile
Horizontal Inheritance
• Trait
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);

}

}
Horizontal Inheritance
• Trait

- Promotes code reuse

- Unrestricted by inheritance hierarchies

- Available since PHP 5.4.0
Horizontal Inheritance
• Trait

- Pull in grouped functionality

- Multiple inheritance horizontally

- Similar to a concrete class but cannot be instantiated
Horizontal Inheritance
• Trait

- “Aware” of a concept

‣ ContainerAware

‣ AuditAware

‣ AdapterAware
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
Horizontal Inheritance
• Trait

- Can not be instantiated

- Can not implement an interface

- Can not extend a class
Horizontal Inheritance
• Trait

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

- Traits can not have class constants
Horizontal Inheritance
• Trait

- Order of precedence

‣ Class and trait method naming collisions

• Current class

• Trait

• Parent class
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.’; }

}
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(); }

}
Horizontal Inheritance
• Trait

- Naming conflicts must be resolved

‣ Fatal error if not
Horizontal Inheritance
• Trait

- ‘insteadof’ to use just one

- ‘as’ to alias and allow using more than one
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();
Horizontal Inheritance
• Trait

- Can change visibility modifier

‣ use Trait { methodName as protected; }

‣ class Thing {

use Trait {

Trait::doThing as public;

}

}

$thing->doThing();
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’;

}
Horizontal Inheritance
• Trait

- Traits can use other traits

‣ trait Thing {

use ThingA, ThingB;

}
Horizontal Inheritance
• Trait

- Traits can have abstract methods

‣ trait Thing {

abstract protected function doSomething();

}

class X {

use Thing;

protected function doSomething() {

echo ‘doing something’;

}

}
Horizontal Inheritance
• Trait

- Traits can have static methods

‣ trait Thing {

public static function anotherThing();

}



class X { use Thing; }



X::anotherThing();
Horizontal Inheritance
• Trait

- Recap

‣ Overcome multiple inheritance problem

‣ Horizontal inheritance
Horizontal Inheritance
Applied Use
Applied Use
• Vertical or Horizontal inheritance?

- Vertical: abstract, interface

- Horizontal: trait
Applied Use
• Vertical or Horizontal inheritance?

- Vertical is heavily used

- Most concrete classes extend an abstract in projects
Applied Use
• Vertical or Horizontal inheritance?

- My experience

‣ Most use vertical

‣ Few use horizontal
Applied Use
• Vertical or Horizontal inheritance?

- My experience with concrete classes

‣ Most extend an abstract

‣ Some implement an interface

‣ Few use trait
Considerations
Considerations
• Open discussion
Considerations
• Why need for abstracts, interfaces, and traits
Considerations
• Benefits of using combinations of abstracts, interfaces, and
traits
Considerations
• Traits vs Dependency Injection
Considerations
• Role of Dependency Injection
Considerations
• Code complexity

- How far to take abstraction

- Readability

- Code consolidation
Considerations
• Code testability

- Unit tests

- Testing abstract instead of concrete implementation
Considerations
• Evaluating current projects

- Static code analysis

- Leverage inheritance
Questions?
• Please rate this talk

- https://joind.in/talk/77415
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

More Related Content

PPTX
Scala in practice
PDF
Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...
PDF
Software Engineering Thailand: Programming with Scala
KEY
Java to Scala: Why & How
PDF
Scala for android
PDF
JAZOON'13 - Paul Brauner - A backend developer meets the web: my Dart experience
PPTX
What To Leave Implicit
KEY
Java to scala
Scala in practice
Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...
Software Engineering Thailand: Programming with Scala
Java to Scala: Why & How
Scala for android
JAZOON'13 - Paul Brauner - A backend developer meets the web: my Dart experience
What To Leave Implicit
Java to scala

Viewers also liked (9)

PDF
Developing applications for performance
PDF
Web Performance 2017: Myths and Truths (php[world] 2017)
PDF
Create a PHP Library the right way
PPTX
Essential Tools for Modern PHP
PDF
Leveraging a distributed architecture to your advantage
PDF
Webpack Encore Symfony Live 2017 San Francisco
PDF
A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017
PDF
Advanced MySQL Query Optimizations
PDF
MySQL 8.0 Preview: What Is Coming?
Developing applications for performance
Web Performance 2017: Myths and Truths (php[world] 2017)
Create a PHP Library the right way
Essential Tools for Modern PHP
Leveraging a distributed architecture to your advantage
Webpack Encore Symfony Live 2017 San Francisco
A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017
Advanced MySQL Query Optimizations
MySQL 8.0 Preview: What Is Coming?
Ad

Similar to Inheritance: Vertical or Horizontal (20)

PDF
Advanced PHP Simplified - Sunshine PHP 2018
PPTX
Ritik (inheritance.cpp)
PPTX
OOPS Characteristics (With Examples in PHP)
PPTX
Inheritance.pptx
PPTX
Object Oriented Design and Programming Unit-03
PPTX
inheritance in OOPM
PDF
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
PPTX
Inheritance
PPTX
inheritance in C++
PPTX
OOP-part-2 object oriented programming.pptx
PDF
OOP Inheritance
PDF
Inheritance and Substitution
PDF
Unit 3 notes.pdf
PPTX
Inheritance in oops
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT III (8).pptx
PPTX
UNIT III (8).pptx
PPTX
2.6 Types of Inheritance in OOP C++.pptx
PDF
Inheritance in C++ Programming Language
Advanced PHP Simplified - Sunshine PHP 2018
Ritik (inheritance.cpp)
OOPS Characteristics (With Examples in PHP)
Inheritance.pptx
Object Oriented Design and Programming Unit-03
inheritance in OOPM
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Inheritance
inheritance in C++
OOP-part-2 object oriented programming.pptx
OOP Inheritance
Inheritance and Substitution
Unit 3 notes.pdf
Inheritance in oops
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT III (8).pptx
UNIT III (8).pptx
2.6 Types of Inheritance in OOP C++.pptx
Inheritance in C++ Programming Language
Ad

More from Mark Niebergall (20)

PDF
Filesystem Management with Flysystem - php[tek] 2023
PDF
Leveling Up With Unit Testing - php[tek] 2023
PDF
Filesystem Management with Flysystem at PHP UK 2023
PDF
Leveling Up With Unit Testing - LonghornPHP 2022
PDF
Developing SOLID Code
PDF
Unit Testing from Setup to Deployment
PDF
Stacking Up Middleware
PDF
BDD API Tests with Gherkin and Behat
PDF
BDD API Tests with Gherkin and Behat
PDF
Hacking with PHP
PDF
Relational Database Design Bootcamp
PDF
Starting Out With PHP
PDF
Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)
PDF
Debugging PHP with Xdebug - PHPUK 2018
PDF
Defensive Coding Crash Course Tutorial
PDF
Cybersecurity State of the Union
PDF
Cryptography With PHP - ZendCon 2017 Workshop
PDF
Defensive Coding Crash Course - ZendCon 2017
PDF
Leveraging Composer in Existing Projects
PDF
Defensive Coding Crash Course
Filesystem Management with Flysystem - php[tek] 2023
Leveling Up With Unit Testing - php[tek] 2023
Filesystem Management with Flysystem at PHP UK 2023
Leveling Up With Unit Testing - LonghornPHP 2022
Developing SOLID Code
Unit Testing from Setup to Deployment
Stacking Up Middleware
BDD API Tests with Gherkin and Behat
BDD API Tests with Gherkin and Behat
Hacking with PHP
Relational Database Design Bootcamp
Starting Out With PHP
Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)
Debugging PHP with Xdebug - PHPUK 2018
Defensive Coding Crash Course Tutorial
Cybersecurity State of the Union
Cryptography With PHP - ZendCon 2017 Workshop
Defensive Coding Crash Course - ZendCon 2017
Leveraging Composer in Existing Projects
Defensive Coding Crash Course

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
System and Network Administraation Chapter 3
PDF
top salesforce developer skills in 2025.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Introduction to Artificial Intelligence
PDF
AI in Product Development-omnex systems
PDF
medical staffing services at VALiNTRY
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
Nekopoi APK 2025 free lastest update
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
System and Network Administraation Chapter 3
top salesforce developer skills in 2025.pdf
Operating system designcfffgfgggggggvggggggggg
Design an Analysis of Algorithms I-SECS-1021-03
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Understanding Forklifts - TECH EHS Solution
How to Migrate SBCGlobal Email to Yahoo Easily
CHAPTER 2 - PM Management and IT Context
Introduction to Artificial Intelligence
AI in Product Development-omnex systems
medical staffing services at VALiNTRY
PTS Company Brochure 2025 (1).pdf.......
Navsoft: AI-Powered Business Solutions & Custom Software Development
ManageIQ - Sprint 268 Review - Slide Deck
How to Choose the Right IT Partner for Your Business in Malaysia
Which alternative to Crystal Reports is best for small or large businesses.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Upgrade and Innovation Strategies for SAP ERP Customers

Inheritance: Vertical or Horizontal