Don’t Be STUPID
Grasp SOLID!
Anthony Ferrara
NorthEastPHP 2013
Let’s Talk
Object
Oriented
Programming
What
Is An
Object?
Classic View
Object == Physical “Thing”
Classic View
Object == Physical “Thing”
Methods == Actions on “Thing”
Classic View
Object == Physical “Thing”
Methods == Actions on “Thing”
Properties == Description of “Thing”
Animal
MammalBird Fish
CatCow Dog
Lion Feline Cheetah
Classic View
$lion = new Lion;
$lion->roar();
$lion->walkTo($point);
$lion->hunt($zebra);
$lion->sleep();
Is This Realistic?
Classic View
$lion = new Lion;
$lion->roar();
$lion->walkTo($point);
$lion->hunt($zebra);
$lion->sleep();
Classic View
$lion = new Lion;
$lion->roar();
$lion->walkTo($point);
$lion->hunt($zebra);
$lion->sleep();
Does A Lion Have
A Button To Make
It Roar?
Classic View
$lion = new Lion;
$lion->roar();
$lion->walkTo($point);
$lion->hunt($zebra);
$lion->sleep();
Does A Lion Have
A Button To Make
It Roar?What Does It
Mean For An
Object To “Hunt”?
Classic View
$lion = new Lion;
$lion->roar();
$lion->walkTo($point);
$lion->hunt($zebra);
$lion->sleep();
Does A Lion Have
A Button To Make
It Roar?What Does It
Mean For An
Object To “Hunt”?
What Is A Lion In
Relation To Our
Application?
The Classical
Model Is Easy To
Understand
Don't be STUPID, Grasp SOLID - North East PHP
The Classical
Model Is
Completely
Impractical
“Modern” View
Object == Collection Of (Related)
Behaviors
“Modern” View
Object == Collection Of (Related)
Behaviors
Methods == Behavior
“Modern” View
Object == Collection Of (Related)
Behaviors
Methods == Behavior
Properties == Details Of Behavior
Classic View == “(conceptually) is a”
Modern View == “behaves as a”
interface Number {
function getValue();
function __toString();
function add(Number $n);
function subtract(Number $n);
function equals(Number $n);
function isLessThan(Number $n);
function isGreaterThan(Number $n);
}
Number
IntegerFloat Decimal
longshort long long
unsigned signed
But Wait!
We Don’t Even
Need Inheritance
All We Need Is
Polymorphism
And Encapsulation
Don't be STUPID, Grasp SOLID - North East PHP
Polymorphism
Behavior Is Determined Dynamically
“Dynamic Dispatch”
Procedural Code
if ($a instanceof Long) {
return new Long($a->getValue() + 1);
} elseif ($a instanceof Float) {
return new Float($a->getValue() + 1.0);
} elseif ($a instanceof Decimal) {
return new Decimal($a->getValue() +
1.0);
}
Polymorphic Code
return $int->add(new Integer(1));
Polymorphic Code
class Integer implements Number {
public function add(Number $a) {
return new Integer(
$this->getValue() +
(int) $a->getValue()
);
}
}
Polymorphic Code
class Float implements Number {
public function add(Number $a) {
return new Float(
$this->getValue() +
(float) $a->getValue()
);
}
}
Encapsulation
Behavior Is Completely Contained By
The Object’s API
(Information Hiding)
Procedural Code
if (5 == $number->value) {
print “Number Is 5”;
} else {
print “Number Is Not 5”;
}
Encapsulated Code
if ($number->equals(new Integer(5))) {
print “Number Is 5”;
} else {
print “Number Is Not 5”;
}
Encapsulated Code
class Decimal implements Number {
protected $intValue;
protected $exponent;
public function equals(Number $a) {
if ($a instanceof Decimal) {
// Compare Directly
} else {
// Cast
}
}
}
Behavior Is
Defined By The
API
Two Types Of Primitive APIs
Interfaces (Explicit)
Two Types Of Primitive APIs
Interfaces (Explicit)
Duck Typing (Implicit)
If an Object Is A
Collection Of
Behaviors...
What Is A
Collection Of
Classes/Objects?
APIs
Method
APIs
Method MethodMethod
Class
APIs
Method MethodMethod
Class ClassClass
Package
APIs
Method MethodMethod
Class ClassClass
Package PackagePackage
Library
APIs
Method MethodMethod
Class ClassClass
Package PackagePackage
Library
Framework
LibraryLibrary
Don't be STUPID, Grasp SOLID - North East PHP
What Makes A
Good API?
A Good API
Does One Thing
A Good API
Never Changes
A Good API
Behaves Like Its
Contract
A Good API
Has A Narrow
Responsibility
A Good API
Depends Upon
Abstractions
And That’s
SOLID
Code
S - Single Responsibility Principle
O-
L -
I -
D-
A Good API
Does One
Thing
Don't be STUPID, Grasp SOLID - North East PHP
S - Single Responsibility Principle
O- Open / Closed Principle
L -
I -
D-
A Good API
Never Changes
Don't be STUPID, Grasp SOLID - North East PHP
S - Single Responsibility Principle
O- Open / Closed Principle
L - Liskov Substitution Principle
I -
D-
A Good API
Behaves Like
Its Contract
Don't be STUPID, Grasp SOLID - North East PHP
Don't be STUPID, Grasp SOLID - North East PHP
Don't be STUPID, Grasp SOLID - North East PHP
S - Single Responsibility Principle
O- Open / Closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D-
A Good API
Has A Narrow
Responsibility
Don't be STUPID, Grasp SOLID - North East PHP
S - Single Responsibility Principle
O- Open / Closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D- Dependency Inversion Principle
A Good API
Depends Upon
Abstractions
Don't be STUPID, Grasp SOLID - North East PHP
S - Single Responsibility Principle
O- Open / Closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D- Dependency Inversion Principle
Note That SOLID
Does Not Dictate
What Is Good OOP
SOLID Emerges
From Good OOP
Don't be STUPID, Grasp SOLID - North East PHP
So, What Makes
A Bad API?
Global Variables
(Spooky Action
At A Distance)
Depending On
Specifics Of An
Implementation
Hidden
Dependencies
Unhealthy Focus
On Performance
Poorly Named
APIs
Duplication
And That’s
STUPID
Code
S - Singletons
T -
U -
P -
I -
D-
Global Variables
(Spooky Action
At A Distance)
Don't be STUPID, Grasp SOLID - North East PHP
S - Singletons
T - Tight Coupling
U -
P -
I -
D-
Depending On
Specifics Of An
Implementation
Don't be STUPID, Grasp SOLID - North East PHP
S - Singletons
T - Tight Coupling
U - Untestable Code
P -
I -
D-
Hidden
Dependencies
Don't be STUPID, Grasp SOLID - North East PHP
S - Singletons
T - Tight Coupling
U - Untestable Code
P - Premature Optimization
I -
D-
Unhealthy Focus
On Performance
Don't be STUPID, Grasp SOLID - North East PHP
S - Singletons
T - Tight Coupling
U - Untestable Code
P - Premature Optimization
I - Indescriptive Naming
D-
Poorly Named
APIs
Don't be STUPID, Grasp SOLID - North East PHP
S - Singletons
T - Tight Coupling
U - Untestable Code
P - Premature Optimization
I - Indescriptive Naming
D- Duplication
Duplication
Duplication
DuplicationDuplication
Duplication
Duplication
Duplication
Duplication
Don't be STUPID, Grasp SOLID - North East PHP
S - Singletons
T - Tight Coupling
U - Untestable Code
P - Premature Optimization
I - Indescriptive Naming
D- Duplication
STUPID
Embodies
Lessons Learned
From Bad OOP
What Good OOP Gives You
Modular Code
Reusable Code
Extendable Code
Easy To Read Code
Maintainable Code
Easy To Change Code
Easy To Understand Code
Clean Abstractions (mostly)
What Good OOP Costs You
Tends To Have More “Layers”
Tends To Be Slower At Runtime
Tends To Have Larger Codebases
Tends To Result In Over-Abstraction
Tends To Require More Effort To Write
Tends To Require More Tacit Knowledge
Let’s Look At
Some Code!
interface Car {
public function turnLeft();
public function turnRight();
public function goFaster();
public function goSlower();
public function shiftUp();
public function shiftDown();
public function start();
}
interface Steerable {
public function steer($angle);
}
interface Acceleratable {
public function accelerate($amt);
}
interface Shiftable {
public function shiftDown();
public function shiftUp();
}
interface Transmission {
public function getNumberOfGears();
public function getGear();
public function shiftToNeutral();
public function shiftToReverse();
public function shiftToGear($gear);
}
interface Manual extends Transmission {
public function engageClutch();
public function releaseClutch();
}
interface Automatic extends Transmission {
public function shiftToDrive();
}
Principle Of
Good
Enough
Don't be STUPID, Grasp SOLID - North East PHP
Don't be STUPID, Grasp SOLID - North East PHP
Don't be STUPID, Grasp SOLID - North East PHP
Don't be STUPID, Grasp SOLID - North East PHP
Don't be STUPID, Grasp SOLID - North East PHP
Anthony Ferrara
joind.in/8907
@ircmaxell
blog.ircmaxell.com
me@ircmaxell.com
youtube.com/ircmaxell

More Related Content

PDF
Beyond design patterns phpnw14
PDF
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
PPT
Functions in php
PDF
Java 8 Streams and Rx Java Comparison
PDF
JDK8 : parallel programming made (too ?) easy
PDF
Free your lambdas
PDF
Javaz. Functional design in Java 8.
PDF
IoC&Laravel
Beyond design patterns phpnw14
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Functions in php
Java 8 Streams and Rx Java Comparison
JDK8 : parallel programming made (too ?) easy
Free your lambdas
Javaz. Functional design in Java 8.
IoC&Laravel

What's hot (20)

PPTX
Aspects of love slideshare
PDF
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
PDF
Functional Programming Essentials
PDF
Introduction to functional programming (In Arabic)
PDF
Java 8 Lambda Expressions & Streams
PDF
How to get along with implicits
PPTX
The Sincerest Form of Flattery
PPTX
Clean Code Principles
PDF
Java 8 Stream API and RxJava Comparison
PDF
Free your lambdas
PDF
The Functional Programming Toolkit (NDC Oslo 2019)
PDF
Going reactive in java
PDF
Java 8, Streams & Collectors, patterns, performances and parallelization
PPTX
Functional programming with Java 8
PDF
Reinventing the Transaction Script (NDC London 2020)
PDF
Building confidence in concurrent code with a model checker: TLA+ for program...
PPTX
Java 8 presentation
PDF
Booting into functional programming
PPT
Introduction To Functional Programming
PDF
Functional Java 8 - Introduction
Aspects of love slideshare
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Functional Programming Essentials
Introduction to functional programming (In Arabic)
Java 8 Lambda Expressions & Streams
How to get along with implicits
The Sincerest Form of Flattery
Clean Code Principles
Java 8 Stream API and RxJava Comparison
Free your lambdas
The Functional Programming Toolkit (NDC Oslo 2019)
Going reactive in java
Java 8, Streams & Collectors, patterns, performances and parallelization
Functional programming with Java 8
Reinventing the Transaction Script (NDC London 2020)
Building confidence in concurrent code with a model checker: TLA+ for program...
Java 8 presentation
Booting into functional programming
Introduction To Functional Programming
Functional Java 8 - Introduction
Ad

Viewers also liked (10)

PPTX
Design Patterns: From STUPID to SOLID code
KEY
SOLID Principles
KEY
SOLID Ruby, SOLID Rails
KEY
Programming SOLID
PDF
Don't Be STUPID, Grasp SOLID - ConFoo Edition
ODP
PDF
Software_Architectures_from_SOA_to_MSA
PPTX
Zero to SOLID
PPTX
GRASP – паттерны Объектно-Ориентированного Проектирования
PPTX
SOLID -Clean Code For Mere Mortals
Design Patterns: From STUPID to SOLID code
SOLID Principles
SOLID Ruby, SOLID Rails
Programming SOLID
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Software_Architectures_from_SOA_to_MSA
Zero to SOLID
GRASP – паттерны Объектно-Ориентированного Проектирования
SOLID -Clean Code For Mere Mortals
Ad

Similar to Don't be STUPID, Grasp SOLID - North East PHP (20)

ODP
Writing Maintainable Perl
PPTX
SADI in Perl - Protege Plugin Tutorial (fixed Aug 24, 2011)
KEY
SPL, not a bridge too far
PPT
Basic PHP
DOC
Jsphp 110312161301-phpapp02
PDF
Typed Properties and more: What's coming in PHP 7.4?
PPTX
Chap1introppt2php(finally done)
PDF
Crossing the Bridge: Connecting Rails and your Front-end Framework
PDF
Scripting3
PDF
The Joy Of Ruby
PPT
Introduction to php
PDF
PPT
PDF
Short intro to ECMAScript
KEY
Rails for PHP Developers
PPT
Object Oriented Software Design Principles
PDF
JavaScript for PHP developers
PDF
Perl 5.10 for People Who Aren't Totally Insane
PDF
Generating Power with Yield
PDF
Functional Programming in R
Writing Maintainable Perl
SADI in Perl - Protege Plugin Tutorial (fixed Aug 24, 2011)
SPL, not a bridge too far
Basic PHP
Jsphp 110312161301-phpapp02
Typed Properties and more: What's coming in PHP 7.4?
Chap1introppt2php(finally done)
Crossing the Bridge: Connecting Rails and your Front-end Framework
Scripting3
The Joy Of Ruby
Introduction to php
Short intro to ECMAScript
Rails for PHP Developers
Object Oriented Software Design Principles
JavaScript for PHP developers
Perl 5.10 for People Who Aren't Totally Insane
Generating Power with Yield
Functional Programming in R

More from Anthony Ferrara (8)

PDF
Password Storage And Attacking In PHP - PHP Argentina
PDF
Development By The Numbers - ConFoo Edition
PDF
PHP, Under The Hood - DPC
PDF
Development by the numbers
PDF
Don't Be Stupid, Grasp Solid - MidWestPHP
PDF
Cryptography For The Average Developer - Sunshine PHP
PDF
Password Storage and Attacking in PHP
PDF
Cryptography For The Average Developer
Password Storage And Attacking In PHP - PHP Argentina
Development By The Numbers - ConFoo Edition
PHP, Under The Hood - DPC
Development by the numbers
Don't Be Stupid, Grasp Solid - MidWestPHP
Cryptography For The Average Developer - Sunshine PHP
Password Storage and Attacking in PHP
Cryptography For The Average Developer

Recently uploaded (20)

PDF
A novel scalable deep ensemble learning framework for big data classification...
PPTX
observCloud-Native Containerability and monitoring.pptx
PPTX
The various Industrial Revolutions .pptx
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
Architecture types and enterprise applications.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
STKI Israel Market Study 2025 version august
PDF
Five Habits of High-Impact Board Members
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PPT
What is a Computer? Input Devices /output devices
PDF
Unlock new opportunities with location data.pdf
PDF
1 - Historical Antecedents, Social Consideration.pdf
A novel scalable deep ensemble learning framework for big data classification...
observCloud-Native Containerability and monitoring.pptx
The various Industrial Revolutions .pptx
Zenith AI: Advanced Artificial Intelligence
Developing a website for English-speaking practice to English as a foreign la...
Architecture types and enterprise applications.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Group 1 Presentation -Planning and Decision Making .pptx
A comparative study of natural language inference in Swahili using monolingua...
O2C Customer Invoices to Receipt V15A.pptx
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
STKI Israel Market Study 2025 version august
Five Habits of High-Impact Board Members
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Final SEM Unit 1 for mit wpu at pune .pptx
What is a Computer? Input Devices /output devices
Unlock new opportunities with location data.pdf
1 - Historical Antecedents, Social Consideration.pdf

Don't be STUPID, Grasp SOLID - North East PHP