SlideShare a Scribd company logo
1 of 23
Download to read offline
SOLID
OOP paradigms
● Basic principles
○ Favor Composition over Inheritance
○ Law of Demeter
○ Dependency Injection
● S.O.L.I.D. principles
SOLID
OOP paradigms
SOLID
Favor Composition over Inheritance
● Inheritance generates LOTS of problems
○ Ex: Circle-Ellipse problem
■ Class Ellipse with radiusX, radiusY
■ Class Circle extends from Ellipse
■ later add stretchX()
SOLID
Favor Composition over Inheritance
● Minimize coupling (least knowledge principle)
● In a class method you can use:
○ 1. Object itself
○ 2. Method’s parameters
○ 3. Objects created inside
○ 4. Any class’ components (properties+methods)
SOLID
Law of Demeter (LoD)
● “Don’t ask your dog to move legs but walk”
○ As code: dog.leg.move()
○ You do not care how your dog walk
○ Only the dog knows the way it walks
SOLID
Law of Demeter (LoD)
● What if the XML is located in a DB?
SOLID
Dependency Injection
class FeedParser
{
public function __construct($filename)
{
$this->XmlFilename = $filename;
}
public function doParse()
{
$xmlData = $this->readXML($this->XmlFilename);
$products = $this->extractProductsFromXmlData($xmlData);
return $products;
}
private function readXML($filename)
{
$xmlData = simplexml_load_file($filename);
//(... Guard clauses ...)
return $xmlData;
}
● One object supplies the dependencies of another object
SOLID
Dependency Injection
class FeedParser
{
private $xmlReader;
public function __construct(XmlReader $xmlReader)
{
$this->xmlReader = $xmlReader;
}
public function doParse()
{
$xmlData = $this->readXML();
$products = $this->extractProductsFromXmlData($xmlData);
return $products;
}
private function readXML()
{
$xmlData = $this->xmlReader->read();
//(... Guard clauses ...)
● When a new object is build, you can set concrete dependencies
SOLID
Dependency Injection
class FeedParser
{
private $xmlReader;
public function __construct(XmlReader $xmlReader)
{
$this->xmlReader = $xmlReader;
}
//(...)
}
class XmlReader
{
//(...)
}
$myXmlReader = new XMLReader($filename);
$myParser = new FeedParser($myXmlReader);
● Improves decoupling
● Avoid hardcoding dependencies
● [Hint] Look for:
○ new Class()
○ build-in functions
■ Ex. date(), fopen()
SOLID
Dependency Injection
● Mnemonic acronym (by Feathers)
● “first five principles” of OOP design (by Uncle Bob)
SOLID
SOLID
● A class should have only a single responsibility
SOLID
S: Single Responsibility Principle (SRP)
public function doParse()
{
$xmlData = $this->readXML();
$products = $this->extractProductsFromXmlData($xmlData);
return $products;
}
private function readXML()
{
$this->checkFileExists($filename);
$xmlData = $this->xmlReader->read();
$this->checkXmlHasContent($xmlData);
return $xmlData;
}
● A class should have only a single responsibility
SOLID
S: Single Responsibility Principle (SRP)
public function doParse()
{
$xmlData = $this->xmlReader->read();
$products = $this->extractProductsFromXmlData($xmlData);
return $products;
}
private function readXML()
{
$this->checkFileExists($filename);
$xmlData = $this->xmlReader->read();
$this->checkXmlHasContent($xmlData);
return $xmlData;
}
● Software entities (classes, modules, etc) should be:
○ Open for extension
■ Doesn’t mean inheritance
○ Closed for modification
■ No need to touch the original code
● How could we do so?
SOLID
O: Open-Closed Principle
● What if our data comes from a CSV? And later from a JSON?
SOLID
O: Open-Closed Principle
class FeedParser
{
private $xmlReader;
public function __construct(XmlReader $xmlReader)
{
$this->xmlReader = $xmlReader;
}
public function doParse()
{
$xmlData = $this->xmlReader->read();
//(...)
SOLID
O: Open-Closed Principle, using interfaces!
class FeedParser
{
private $reader;
public function __construct(DataReaderWriter $reader)
{
$this->reader = $reader;
}
public function doParse()
{
$xmlData = $this->reader->read();
//(...)
}
}
interface DataReaderWriter
{
public function read();
public function write($data);
}
class XMLReaderWriter implements DataReaderWriter
{ // ... implement read() and write() }
● If ChildClass is a subtype of ParentClass, then a ParentClass object
can replace a ChildClass object
● Remember Circle-Ellipse problem
● “Avoid most inheritance cases”
SOLID
L: Liskov Substitution Principle
● No client should be forced to depend on methods it doesn’t use
● “Split a large interface in separated small interfaces”
SOLID
I: Interface Segregation Principle
SOLID
I: Interface Segregation Principle
class FeedParser
{
private $reader;
public function __construct(DataReaderWriter $reader)
{
$this->reader = $reader;
}
public function doParse()
{
$xmlData = $this->reader->read();
//(...)
}
}
interface DataReaderWriter
{
public function read();
public function write($data);
}
class XMLReaderWriter implements DataReaderWriter
{ // ... implement read() and write() }
SOLID
I: Interface Segregation Principle
class FeedParser
{
private $reader;
public function __construct(DataReader $reader)
{
$this->reader = $reader;
}
public function doParse()
{
$xmlData = $this->reader->read();
//(...)
}
}
interface DataReader
{
public function read();
}
class XMLReaderWriter implements DataReader, DataWriter
{ // ... implement read() and write() }
● High-level modules should not depend on low-level modules.
Both should depend on abstractions.
● Does it look familiar?
SOLID
D: Dependency Inversion Principle
● We have already used it!
SOLID
D: Dependency Inversion Principle
class FeedParser
{
private $xmlReader;
public function __construct(XmlReader $xmlReader)
{
$this->xmlReader = $xmlReader;
}
//-----
class FeedParser
{
private $reader;
public function __construct(DataReaderWriter $reader)
{
$this->reader = $reader;
}
● Books:
○ Head First OO Analysis and Design (McLaughlin)
○ Head First Design Patterns (Freeman)
● Internet:
○ https://sites.google.com/site/unclebobconsultingllc/getting-a-solid-start
■ About SOLID: “They are not laws. They are not perfect truths.”
SOLID
References

More Related Content

What's hot

What's hot (20)

Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful software
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
PHP Traits
PHP TraitsPHP Traits
PHP Traits
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
 
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
 
Php variables
Php variablesPhp variables
Php variables
 
The State of Lithium
The State of LithiumThe State of Lithium
The State of Lithium
 
Object Calisthenics Applied to PHP
Object Calisthenics Applied to PHPObject Calisthenics Applied to PHP
Object Calisthenics Applied to PHP
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix it
 
Java and XML Schema
Java and XML SchemaJava and XML Schema
Java and XML Schema
 
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
PHP for Adults: Clean Code and Object Calisthenics
PHP for Adults: Clean Code and Object CalisthenicsPHP for Adults: Clean Code and Object Calisthenics
PHP for Adults: Clean Code and Object Calisthenics
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of Lithium
 

Viewers also liked

Object Oriented Terms
Object Oriented TermsObject Oriented Terms
Object Oriented Terms
guest3c69aefa
 
Solid and ioc principles
Solid and ioc principlesSolid and ioc principles
Solid and ioc principles
eleksdev
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 

Viewers also liked (20)

OOP & SOLID
OOP & SOLIDOOP & SOLID
OOP & SOLID
 
Advanced PHP Concepts - Tutorial 2 of 3
Advanced PHP Concepts - Tutorial 2 of 3Advanced PHP Concepts - Tutorial 2 of 3
Advanced PHP Concepts - Tutorial 2 of 3
 
Object Oriented Terms
Object Oriented TermsObject Oriented Terms
Object Oriented Terms
 
Procedural to oop in php
Procedural to oop in phpProcedural to oop in php
Procedural to oop in php
 
Introduction to Docker
Introduction to DockerIntroduction to Docker
Introduction to Docker
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
 
Professional development
Professional developmentProfessional development
Professional development
 
Solid and ioc principles
Solid and ioc principlesSolid and ioc principles
Solid and ioc principles
 
Clean code
Clean codeClean code
Clean code
 
Advanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceAdvanced java practical semester 6_computer science
Advanced java practical semester 6_computer science
 
Clean Code
Clean CodeClean Code
Clean Code
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Java
 
OOP java
OOP javaOOP java
OOP java
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Java programming-examples
Java programming-examplesJava programming-examples
Java programming-examples
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop)   cs304 power point slides lecture 01Object oriented programming (oop)   cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 

Similar to Some OOP paradigms & SOLID

Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented design
Jean Michel
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHP
mtoppa
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
Svetlin Nakov
 

Similar to Some OOP paradigms & SOLID (20)

Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented design
 
Advanced PHP Simplified - Sunshine PHP 2018
Advanced PHP Simplified - Sunshine PHP 2018Advanced PHP Simplified - Sunshine PHP 2018
Advanced PHP Simplified - Sunshine PHP 2018
 
OOP
OOPOOP
OOP
 
Migration from Procedural to OOP
Migration from Procedural to OOP Migration from Procedural to OOP
Migration from Procedural to OOP
 
PHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOPPHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOP
 
SOLID
SOLIDSOLID
SOLID
 
SOLID
SOLIDSOLID
SOLID
 
What is DDD and how could it help you
What is DDD and how could it help youWhat is DDD and how could it help you
What is DDD and how could it help you
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHP
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
 
Solid principles in action
Solid principles in actionSolid principles in action
Solid principles in action
 
Revisiting SOLID Principles
Revisiting  SOLID Principles Revisiting  SOLID Principles
Revisiting SOLID Principles
 
Alexander Makarov "Let’s talk about code"
Alexander Makarov "Let’s talk about code"Alexander Makarov "Let’s talk about code"
Alexander Makarov "Let’s talk about code"
 
Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
 
Advanced PHP Simplified
Advanced PHP SimplifiedAdvanced PHP Simplified
Advanced PHP Simplified
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
FluentDom
FluentDomFluentDom
FluentDom
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
 

Recently uploaded

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
UK Journal
 

Recently uploaded (20)

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 

Some OOP paradigms & SOLID

  • 2. ● Basic principles ○ Favor Composition over Inheritance ○ Law of Demeter ○ Dependency Injection ● S.O.L.I.D. principles SOLID OOP paradigms
  • 4. ● Inheritance generates LOTS of problems ○ Ex: Circle-Ellipse problem ■ Class Ellipse with radiusX, radiusY ■ Class Circle extends from Ellipse ■ later add stretchX() SOLID Favor Composition over Inheritance
  • 5. ● Minimize coupling (least knowledge principle) ● In a class method you can use: ○ 1. Object itself ○ 2. Method’s parameters ○ 3. Objects created inside ○ 4. Any class’ components (properties+methods) SOLID Law of Demeter (LoD)
  • 6. ● “Don’t ask your dog to move legs but walk” ○ As code: dog.leg.move() ○ You do not care how your dog walk ○ Only the dog knows the way it walks SOLID Law of Demeter (LoD)
  • 7. ● What if the XML is located in a DB? SOLID Dependency Injection class FeedParser { public function __construct($filename) { $this->XmlFilename = $filename; } public function doParse() { $xmlData = $this->readXML($this->XmlFilename); $products = $this->extractProductsFromXmlData($xmlData); return $products; } private function readXML($filename) { $xmlData = simplexml_load_file($filename); //(... Guard clauses ...) return $xmlData; }
  • 8. ● One object supplies the dependencies of another object SOLID Dependency Injection class FeedParser { private $xmlReader; public function __construct(XmlReader $xmlReader) { $this->xmlReader = $xmlReader; } public function doParse() { $xmlData = $this->readXML(); $products = $this->extractProductsFromXmlData($xmlData); return $products; } private function readXML() { $xmlData = $this->xmlReader->read(); //(... Guard clauses ...)
  • 9. ● When a new object is build, you can set concrete dependencies SOLID Dependency Injection class FeedParser { private $xmlReader; public function __construct(XmlReader $xmlReader) { $this->xmlReader = $xmlReader; } //(...) } class XmlReader { //(...) } $myXmlReader = new XMLReader($filename); $myParser = new FeedParser($myXmlReader);
  • 10. ● Improves decoupling ● Avoid hardcoding dependencies ● [Hint] Look for: ○ new Class() ○ build-in functions ■ Ex. date(), fopen() SOLID Dependency Injection
  • 11. ● Mnemonic acronym (by Feathers) ● “first five principles” of OOP design (by Uncle Bob) SOLID SOLID
  • 12. ● A class should have only a single responsibility SOLID S: Single Responsibility Principle (SRP) public function doParse() { $xmlData = $this->readXML(); $products = $this->extractProductsFromXmlData($xmlData); return $products; } private function readXML() { $this->checkFileExists($filename); $xmlData = $this->xmlReader->read(); $this->checkXmlHasContent($xmlData); return $xmlData; }
  • 13. ● A class should have only a single responsibility SOLID S: Single Responsibility Principle (SRP) public function doParse() { $xmlData = $this->xmlReader->read(); $products = $this->extractProductsFromXmlData($xmlData); return $products; } private function readXML() { $this->checkFileExists($filename); $xmlData = $this->xmlReader->read(); $this->checkXmlHasContent($xmlData); return $xmlData; }
  • 14. ● Software entities (classes, modules, etc) should be: ○ Open for extension ■ Doesn’t mean inheritance ○ Closed for modification ■ No need to touch the original code ● How could we do so? SOLID O: Open-Closed Principle
  • 15. ● What if our data comes from a CSV? And later from a JSON? SOLID O: Open-Closed Principle class FeedParser { private $xmlReader; public function __construct(XmlReader $xmlReader) { $this->xmlReader = $xmlReader; } public function doParse() { $xmlData = $this->xmlReader->read(); //(...)
  • 16. SOLID O: Open-Closed Principle, using interfaces! class FeedParser { private $reader; public function __construct(DataReaderWriter $reader) { $this->reader = $reader; } public function doParse() { $xmlData = $this->reader->read(); //(...) } } interface DataReaderWriter { public function read(); public function write($data); } class XMLReaderWriter implements DataReaderWriter { // ... implement read() and write() }
  • 17. ● If ChildClass is a subtype of ParentClass, then a ParentClass object can replace a ChildClass object ● Remember Circle-Ellipse problem ● “Avoid most inheritance cases” SOLID L: Liskov Substitution Principle
  • 18. ● No client should be forced to depend on methods it doesn’t use ● “Split a large interface in separated small interfaces” SOLID I: Interface Segregation Principle
  • 19. SOLID I: Interface Segregation Principle class FeedParser { private $reader; public function __construct(DataReaderWriter $reader) { $this->reader = $reader; } public function doParse() { $xmlData = $this->reader->read(); //(...) } } interface DataReaderWriter { public function read(); public function write($data); } class XMLReaderWriter implements DataReaderWriter { // ... implement read() and write() }
  • 20. SOLID I: Interface Segregation Principle class FeedParser { private $reader; public function __construct(DataReader $reader) { $this->reader = $reader; } public function doParse() { $xmlData = $this->reader->read(); //(...) } } interface DataReader { public function read(); } class XMLReaderWriter implements DataReader, DataWriter { // ... implement read() and write() }
  • 21. ● High-level modules should not depend on low-level modules. Both should depend on abstractions. ● Does it look familiar? SOLID D: Dependency Inversion Principle
  • 22. ● We have already used it! SOLID D: Dependency Inversion Principle class FeedParser { private $xmlReader; public function __construct(XmlReader $xmlReader) { $this->xmlReader = $xmlReader; } //----- class FeedParser { private $reader; public function __construct(DataReaderWriter $reader) { $this->reader = $reader; }
  • 23. ● Books: ○ Head First OO Analysis and Design (McLaughlin) ○ Head First Design Patterns (Freeman) ● Internet: ○ https://sites.google.com/site/unclebobconsultingllc/getting-a-solid-start ■ About SOLID: “They are not laws. They are not perfect truths.” SOLID References