SlideShare a Scribd company logo
1 of 47
From good to SOLID
How to become a better PHP developer?
Katerina
Trajchevska
● Senior Software Engineer & co-
founder of Adeva
● Remote Work Advocate
● Community Volunteer
● Consultant with startups and
Fortune 500 companies
Software Engineer and co-
founder of Adeva
Disclaimer: Code documentation is
omitted in the following code samples
for simplicity.
In practice, you always want to
document your code.
Single Responsibility Principle
● A class should have one, and only one, reason to change.
● Huge benefit for maintainability and stability of already tested code.
● Uncle Bob’s everyday example: cleaning up your room
● A place for everything and everything in its place.
What’s wrong with this class?
● Multiple responsibilities: it has to know the database structure of 2 different
tables and the output screen for displaying the article.
● 3 Reasons to change:
○ Changing the articles table
○ Changing the comments table
○ Changing the output device
How can we improve this structure?
Gather everything together that has the
same reason to change.
Separate the things that change for a
different reason.
Result: 3 small classes with
very specific names that have
only one reason to change.
Open Closed Principle
● A class should be open for extension but closed for modification.
● Extend functionality by adding new code instead of changing existing code.
● Everyday example: Open Source library
● Goal: Separate the behaviors, so the system can easily be extended, but never
broken.
● Uses the concept of Polymorphism: having a single interface for different
entities.
What’s wrong with this example?
● The module is open for modification: every time we want to extend the
functionality, we change the AreaCalculator class.
● Code is error prone: every time we change the calculate function, all
dependencies need to be retested.
How can we improve this structure?
Separate the extensible
behavior behind an interface
and flip the dependencies
around.
Result: Being able to extend
the system’s behavior without
changing the existing code.
Liskov Substitution Principle
● Any derived class should be able to substitute its parent class without the
consumer knowing it.
● Every class that implements an interface, must be able to substitute any
reference throughout the code that implements that interface.
● Everyday example: Driving
● Goal: Every part of the code should get the expected result no matter what
instance of a class you send to it, given it implements the same interface.
What is wrong with this class?
● Returns a collection (object of objects) while the FileStorage returns an
array.
● Changing the FileStorage instance with an instance of DBStorage will
break the code - it expects an array and doesn’t know how to behave with
anything else it receives.
How can we fix it?
Result: Being able to replace
any class in the code with
another one that implements
the same interface without
changing anything.
Dependency Inversion Principle
● Never depend on anything concrete, only depend on abstractions.
● High level modules should not depend on low level modules. They should
depend on abstractions.
● All boils down to decoupling our code.
● Everyday example: Sockets and plugging electrical devices.
● Goal: to be able to change an implementation easily without altering the code
anyhow.
What’s wrong with this example?
● The Book class depends on MySqlConnection.
● We can’t change the database connection (low level module) without changing
the Book class (high level module).
● The Book class shouldn’t care about what database engine we use.
How can we fix this?
Result: Ability to change low
level modules without
changing the high level
modules.
Interface Segregation Principle
● A client should never be forced to depend on methods it doesn't use.
● Or, a client should never depend on anything more than the method it’s calling.
● Everyday example: Hiking with work backpack
● Goal: changing one method in a class shouldn’t affect classes that don’t
depend on it.
What’s wrong with this class?
● It depends on the whole UserInterface and has knowledge for
unnecessary items, like how to register a user.
● It only needs the getNotifyEmail method.
How do we fix it?
Result: Depending only on
the method we’re calling.
Final words about SOLID
● Contributes to code maintainability and extensibility: saves a lot of
development effort through good software architecture.
● You can apply SOLID principles to class methods and functions as well.
● Engineering principles, not strict rules.
● Always use common sense when applying SOLID principles - beware of too
deep fractionation of your code that makes it hard to maintain.
trajchevska@adevait.com
@ktrajchevska
www.linkedin.com/in/trajchevska

More Related Content

Similar to From Good to SOLID: How to become a better PHP developer

Dependency injection using Google guice
Dependency injection using Google guiceDependency injection using Google guice
Dependency injection using Google guiceAman Verma
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility PrincipleBADR
 
Software Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit DeySoftware Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit DeyCefalo
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopMohammad Shawahneh
 
Evgeniy Khyst - why does software design matter and how to keep it in good shape
Evgeniy Khyst - why does software design matter and how to keep it in good shapeEvgeniy Khyst - why does software design matter and how to keep it in good shape
Evgeniy Khyst - why does software design matter and how to keep it in good shapeAnna Shymchenko
 
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif IqbalCode Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif IqbalCefalo
 
Improving your code design using Java
Improving your code design using JavaImproving your code design using Java
Improving your code design using JavaRoan Brasil Monteiro
 
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting TalkJuly 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talkottawaruby
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2Knoldus Inc.
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytechyannick grenzinger
 
Refactoring: Improving the design of existing code
Refactoring: Improving the design of existing codeRefactoring: Improving the design of existing code
Refactoring: Improving the design of existing codeKnoldus Inc.
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesWalking Tree Technologies
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#Aditya Kumar Rajan
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Ramith Jayasinghe
 
Boston Startup School - OO Design
Boston Startup School - OO DesignBoston Startup School - OO Design
Boston Startup School - OO DesignBryan Warner
 

Similar to From Good to SOLID: How to become a better PHP developer (20)

Dependency injection using Google guice
Dependency injection using Google guiceDependency injection using Google guice
Dependency injection using Google guice
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
 
Software Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit DeySoftware Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit Dey
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
 
Soild principles
Soild principlesSoild principles
Soild principles
 
Evgeniy Khyst - why does software design matter and how to keep it in good shape
Evgeniy Khyst - why does software design matter and how to keep it in good shapeEvgeniy Khyst - why does software design matter and how to keep it in good shape
Evgeniy Khyst - why does software design matter and how to keep it in good shape
 
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif IqbalCode Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
 
Improving your code design using Java
Improving your code design using JavaImproving your code design using Java
Improving your code design using Java
 
OO Design Principles
OO Design PrinciplesOO Design Principles
OO Design Principles
 
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting TalkJuly 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
Refactoring: Improving the design of existing code
Refactoring: Improving the design of existing codeRefactoring: Improving the design of existing code
Refactoring: Improving the design of existing code
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree Technologies
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Boston Startup School - OO Design
Boston Startup School - OO DesignBoston Startup School - OO Design
Boston Startup School - OO Design
 

Recently uploaded

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 

Recently uploaded (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 

From Good to SOLID: How to become a better PHP developer

  • 1. From good to SOLID How to become a better PHP developer?
  • 2. Katerina Trajchevska ● Senior Software Engineer & co- founder of Adeva ● Remote Work Advocate ● Community Volunteer ● Consultant with startups and Fortune 500 companies Software Engineer and co- founder of Adeva
  • 3. Disclaimer: Code documentation is omitted in the following code samples for simplicity. In practice, you always want to document your code.
  • 4. Single Responsibility Principle ● A class should have one, and only one, reason to change. ● Huge benefit for maintainability and stability of already tested code. ● Uncle Bob’s everyday example: cleaning up your room ● A place for everything and everything in its place.
  • 5.
  • 6. What’s wrong with this class? ● Multiple responsibilities: it has to know the database structure of 2 different tables and the output screen for displaying the article. ● 3 Reasons to change: ○ Changing the articles table ○ Changing the comments table ○ Changing the output device How can we improve this structure?
  • 7. Gather everything together that has the same reason to change. Separate the things that change for a different reason.
  • 8.
  • 9.
  • 10.
  • 11. Result: 3 small classes with very specific names that have only one reason to change.
  • 12. Open Closed Principle ● A class should be open for extension but closed for modification. ● Extend functionality by adding new code instead of changing existing code. ● Everyday example: Open Source library ● Goal: Separate the behaviors, so the system can easily be extended, but never broken. ● Uses the concept of Polymorphism: having a single interface for different entities.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. What’s wrong with this example? ● The module is open for modification: every time we want to extend the functionality, we change the AreaCalculator class. ● Code is error prone: every time we change the calculate function, all dependencies need to be retested. How can we improve this structure?
  • 18. Separate the extensible behavior behind an interface and flip the dependencies around.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23. Result: Being able to extend the system’s behavior without changing the existing code.
  • 24. Liskov Substitution Principle ● Any derived class should be able to substitute its parent class without the consumer knowing it. ● Every class that implements an interface, must be able to substitute any reference throughout the code that implements that interface. ● Everyday example: Driving ● Goal: Every part of the code should get the expected result no matter what instance of a class you send to it, given it implements the same interface.
  • 25.
  • 26.
  • 27.
  • 28. What is wrong with this class? ● Returns a collection (object of objects) while the FileStorage returns an array. ● Changing the FileStorage instance with an instance of DBStorage will break the code - it expects an array and doesn’t know how to behave with anything else it receives. How can we fix it?
  • 29.
  • 30.
  • 31. Result: Being able to replace any class in the code with another one that implements the same interface without changing anything.
  • 32. Dependency Inversion Principle ● Never depend on anything concrete, only depend on abstractions. ● High level modules should not depend on low level modules. They should depend on abstractions. ● All boils down to decoupling our code. ● Everyday example: Sockets and plugging electrical devices. ● Goal: to be able to change an implementation easily without altering the code anyhow.
  • 33.
  • 34. What’s wrong with this example? ● The Book class depends on MySqlConnection. ● We can’t change the database connection (low level module) without changing the Book class (high level module). ● The Book class shouldn’t care about what database engine we use. How can we fix this?
  • 35.
  • 36. Result: Ability to change low level modules without changing the high level modules.
  • 37. Interface Segregation Principle ● A client should never be forced to depend on methods it doesn't use. ● Or, a client should never depend on anything more than the method it’s calling. ● Everyday example: Hiking with work backpack ● Goal: changing one method in a class shouldn’t affect classes that don’t depend on it.
  • 38.
  • 39.
  • 40.
  • 41. What’s wrong with this class? ● It depends on the whole UserInterface and has knowledge for unnecessary items, like how to register a user. ● It only needs the getNotifyEmail method. How do we fix it?
  • 42.
  • 43.
  • 44.
  • 45. Result: Depending only on the method we’re calling.
  • 46. Final words about SOLID ● Contributes to code maintainability and extensibility: saves a lot of development effort through good software architecture. ● You can apply SOLID principles to class methods and functions as well. ● Engineering principles, not strict rules. ● Always use common sense when applying SOLID principles - beware of too deep fractionation of your code that makes it hard to maintain.