SlideShare a Scribd company logo
Developing SOLID Code
Mark Niebergall

https://www.slideshare.net/MarkNiebergall/developing-solid-code
Objective
• Understand SOLID principles

• Improve code architecture
Overview
• SOLID Principles

• Practical Examples

• Clean Architecture Bene
fi
ts
SOLID Principles
• 5 Principle of OOP

• Promote clean code architecture

• Basis of stable code
• S - Single-Responsibility

• O - Open/Closed

• L - Liskov Substitution

• I - Interface Segregation

• D - Dependency Inversion
SOLID Principles
SOLID Principles
• S - Single-Responsibility
SOLID Principles
class Square
{
public float $length;
public function calculateArea(): float
{
return $this->length * $this->length;
}
public function calculateCircleArea(float $radius): float
{
return pi() * pow($radius, 2);
}
}
SOLID Principles
abstract class Shape
{
abstract public function calculateArea(): float;
}
class Circle extends Shape
{
public float $radius;
public function calculateArea(): float
{
return pi() * pow($this->radius, 2);
}
}
SOLID Principles
• S - Single-Responsibility

- Do 1 thing

- Do that thing well
SOLID Principles
• S - Single-Responsibility

- Decoupled code

- Easier to test
SOLID Principles
• S - Single-Responsibility

- Entity

- Service

- Factory

- Validator

- Handler

- Middleware

- Action

- Controller

- Response
SOLID Principles
• S - Single-Responsibility

- Worker

- Queue

- Cmd

- Adapter

- Client
SOLID Principles
• S - Single-Responsibility

- Abstract

- Interface

- Trait

- Enum
SOLID Principles
• O - Open/Closed
SOLID Principles
class Pet
{
public function walk(): void
{
$this->putOnLeash();
$this->walkOutside();
}
public function feed($food): void
{
$this->pourFoodInDish($food);
}
}
SOLID Principles
abstract class PetAbstract
{
public function __construct(protected string $name)
{
}
public function getName(): string
{
return $this->name;
}
abstract public function feed(Food $food): void;
}
class Snake extends PetAbstract
{
public function feed(Food $food): void
{
// place food in habitat
}
}
SOLID Principles
• O - Open/Closed

- Open for extension

- Closed for modi
fi
cation
SOLID Principles
• O - Open/Closed

- Extend class instead of altering
SOLID Principles
• L - Liskov Substitution
SOLID Principles
class Duck
{
public function fly(): bool
{
// ducks can fly!
return true;
}
}
class RubberDuck extends Duck
{
public function fly(): bool
{
// rubber ducks can't fly
return false;
}
}
SOLID Principles
• L - Liskov Substitution

- Replaceable by subtypes

- Do not alter parent class
SOLID Principles
• L - Liskov Substitution

- Covariance for return types
SOLID Principles
• I - Interface Segregation
SOLID Principles
interface EmployeeInterface
{
public function checkEmail(): bool;
public function createPullRequest(): PullRequest;
public function attendMeeting(Meeting $meeting): void;
}
SOLID Principles
• I - Interface Segregation

- Many client-speci
fi
c interfaces are better than one
general-purpose interface

- Keep interfaces simple
SOLID Principles
• I - Interface Segregation

- Remember the S for Single-Responsibility
SOLID Principles
• I - Interface Segregation

- Most PHPFIG PSRs are interfaces

‣ HTTP Handlers, Client

‣ Cache

‣ Factories

‣ Logging
SOLID Principles
• D - Dependency Inversion
SOLID Principles
class UserService
{
protected HotmailEmailClient $hotmailEmailClient;
public function setHotmailEmailClient(HotmailEmailClient $hotmailEmailClient): void
{
$this->hotmailEmailClient = $hotmailEmailClient;
}
public function emailUser(User $user): bool
{
$emailMessage = new EmailMessage();
$emailMessage->setEmailAddress($user->getEmailAddress());
return $this->hotmailEmailClient->sendEmail($emailMessage);
}
}
SOLID Principles
class ContactService
{
protected EmailClientInterface $emailClient;
public function setEmailClient(EmailClientInterface $emailClient): void
{
$this->emailClient = $emailClient;
}
public function sendEmail(
Contact $contact,
EmailMessageInterface $emailMessage
): bool {
$emailMessage->setEmailAddress($contact->getEmailAddress());
return $this->emailClient->sendEmail($emailMessage);
}
}
SOLID Principles
• D - Dependency Inversion

- Use interfaces, abstracts rather than concrete classes
SOLID Principles
• D - Dependency Inversion

- Law of Demeter

‣ No reaching through to object’s objects
SOLID Principles
• D - Dependency Inversion

- Promotes code reuse

- Promotes code compatibility
Practical Examples
Practical Examples
class UserEntity implements HydratorInterface, ToArrayInterface
{
protected int $id;
protected string $username;
protected bool $isActive;
public function hydrate(array $data)
{
$this->setId($data['id']);
$this->setUsername($data['username']);
$this->setIsActive($data['is_active']);
}
public function toArray(): array
{
return [
'id' => $this->getId(),
'username' => $this->getUsername(),
'is_active' => $this->getIsActive(),
];
}
Practical Examples
class AuditRequestMiddleware implements MiddlewareInterface
{
public function __construct(protected AuditService $auditService)
{
}
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
$response = $handler->handle($request);
$this->auditService->auditRequest($request, $response);
return $response;
}
}
• Other ideas?
Practical Examples
Clean Architecture Bene
fi
ts
Clean Architecture Bene
fi
ts
• Promotes other best practices

- DDD: Domain Driven Design

- DRY: Don’t Repeat Yourself

- KISS: Keep It Simple, Stupid

- YANGI: You Ain’t Gonna Need It

- OOP: Object Oriented Programming
Clean Architecture Bene
fi
ts
• Promotes code reuse

- Write fewer tests!

- Lower maintenance
Clean Architecture Bene
fi
ts
• Introduce fewer bugs

- Code stabilization

- Less brittle code
Clean Architecture Bene
fi
ts
• Decoupled Code

- Easier architecture to unit test

- Easier to read

- Easier to use

- Easier to upgrade packages

- Easier to swap out third-party providers
Clean Architecture Bene
fi
ts
• More features, less debugging

- Improve team velocity

- More time delivering value

- Less time debugging unstructured code and data
Clean Architecture Bene
fi
ts
• Improved Scalability

- Add new tables, columns

- Already setup to expand domains
Clean Architecture Bene
fi
ts
• Con
fi
dence in authored code

- Reduce unknowns

- Testable code
Clean Architecture Bene
fi
ts
• Other?
Review
• S - Single-Responsibility

• O - Open/Closed

• L - Liskov Substitution

• I - Interface Segregation

• D - Dependency Inversion
SOLID Principles
• Questions?

• Discussion points?
Mark Niebergall @mbniebergall
• PHP since 2005

• Masters degree in MIS

• Senior Software Engineer

• Vulnerability Management project (security scans)

• Utah PHP Co-Organizer

• CSSLP, SSCP Certi
fi
ed and SME

• Endurance sports, outdoors
References
• https://www.hashbangcode.com/article/solid-principles-
php

• https://levelup.gitconnected.com/solid-principles-
simpli
fi
ed-php-examples-based-dc6b4f8861f6

• https://www.digitalocean.com/community/
conceptual_articles/s-o-l-i-d-the-
fi
rst-
fi
ve-principles-of-
object-oriented-design

More Related Content

PDF
Advanced PHP Simplified - Sunshine PHP 2018
PPT
PDF
A Gentle Introduction To Object Oriented Php
PPTX
Oop in-php
PDF
Cfphp Zce 01 Basics
PDF
PHP 8: Process & Fixing Insanity
PPT
Class 7 - PHP Object Oriented Programming
PPT
Functions in php
Advanced PHP Simplified - Sunshine PHP 2018
A Gentle Introduction To Object Oriented Php
Oop in-php
Cfphp Zce 01 Basics
PHP 8: Process & Fixing Insanity
Class 7 - PHP Object Oriented Programming
Functions in php

What's hot (19)

PPTX
Object oreinted php | OOPs
PDF
PHPID online Learning #6 Migration from procedural to OOP
PDF
SOLID Principles
PDF
OOP in PHP
PDF
Drupaljam xl 2019 presentation multilingualism makes better programmers
PDF
OOP in PHP
PPT
PHP - Introduction to Object Oriented Programming with PHP
PDF
Intermediate OOP in PHP
PPT
PHP- Introduction to Object Oriented PHP
PPT
Php Oop
PDF
Nikita Popov "What’s new in PHP 8.0?"
PPTX
Introduction to PHP OOP
PPT
C++ polymorphism
PPT
Intro to OOP and new features in PHP 5.3
PDF
PHP Unit 3 functions_in_php_2
PDF
Typed Properties and more: What's coming in PHP 7.4?
PPT
Groovy presentation
PPT
Class 3 - PHP Functions
PDF
Introduction to Clean Code
Object oreinted php | OOPs
PHPID online Learning #6 Migration from procedural to OOP
SOLID Principles
OOP in PHP
Drupaljam xl 2019 presentation multilingualism makes better programmers
OOP in PHP
PHP - Introduction to Object Oriented Programming with PHP
Intermediate OOP in PHP
PHP- Introduction to Object Oriented PHP
Php Oop
Nikita Popov "What’s new in PHP 8.0?"
Introduction to PHP OOP
C++ polymorphism
Intro to OOP and new features in PHP 5.3
PHP Unit 3 functions_in_php_2
Typed Properties and more: What's coming in PHP 7.4?
Groovy presentation
Class 3 - PHP Functions
Introduction to Clean Code
Ad

Similar to Developing SOLID Code (20)

PPTX
From Good to SOLID: How to become a better PHP developer
PPTX
PDF
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
PDF
Dependency Injection for PHP
ODP
PDF
Object Oriented Design Principles
PDF
Object Oriented Programming for WordPress Plugin Development
PDF
So S.O.L.I.D Fu - Designing Better Code
PPTX
“SOLID principles in PHP – how to apply them in PHP and why should we care“ b...
PDF
Don't Be STUPID, Grasp SOLID - ConFoo Edition
PPTX
Architectural changes in Orion
PPTX
SOLID_Principles_Explained_Presentation.pptx
PDF
Save time by applying clean code principles
PPTX
Becoming a better developer by using the SOLID design principles
PDF
Solid OO & Clean Coding is essential to successful Agile development
PDF
Clean code & design patterns
ODP
Solid and Infrastructure as Code
PDF
Developing solid applications
PDF
Some OOP paradigms & SOLID
PPTX
SOLID Principles of Refactoring Presentation - Inland Empire User Group
From Good to SOLID: How to become a better PHP developer
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Dependency Injection for PHP
Object Oriented Design Principles
Object Oriented Programming for WordPress Plugin Development
So S.O.L.I.D Fu - Designing Better Code
“SOLID principles in PHP – how to apply them in PHP and why should we care“ b...
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Architectural changes in Orion
SOLID_Principles_Explained_Presentation.pptx
Save time by applying clean code principles
Becoming a better developer by using the SOLID design principles
Solid OO & Clean Coding is essential to successful Agile development
Clean code & design patterns
Solid and Infrastructure as Code
Developing solid applications
Some OOP paradigms & SOLID
SOLID Principles of Refactoring Presentation - Inland Empire User Group
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
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
Inheritance: Vertical or Horizontal
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
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
Inheritance: Vertical or Horizontal
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)

PPTX
L1 - Introduction to python Backend.pptx
PDF
System and Network Administraation Chapter 3
PDF
Digital Strategies for Manufacturing Companies
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
System and Network Administration Chapter 2
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
history of c programming in notes for students .pptx
PDF
Nekopoi APK 2025 free lastest update
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
AI in Product Development-omnex systems
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
ai tools demonstartion for schools and inter college
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
L1 - Introduction to python Backend.pptx
System and Network Administraation Chapter 3
Digital Strategies for Manufacturing Companies
ISO 45001 Occupational Health and Safety Management System
Understanding Forklifts - TECH EHS Solution
How to Migrate SBCGlobal Email to Yahoo Easily
System and Network Administration Chapter 2
Which alternative to Crystal Reports is best for small or large businesses.pdf
Operating system designcfffgfgggggggvggggggggg
history of c programming in notes for students .pptx
Nekopoi APK 2025 free lastest update
CHAPTER 2 - PM Management and IT Context
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Odoo POS Development Services by CandidRoot Solutions
AI in Product Development-omnex systems
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PTS Company Brochure 2025 (1).pdf.......
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
ai tools demonstartion for schools and inter college
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx

Developing SOLID Code