SlideShare a Scribd company logo
1 of 22
The Singleton Dilemma




Frank Sons
Senior PHP Engineer CTD
The Singleton Dilemma


              What is a Singleton?



             What is the problem?



      What is the solution to this dilemma?




                                              2
Singleton? What‘s that?




                          3
Singleton? Tell me more…


       Probably best known design pattern



      Only one instance of a class at any time



       Global access to that single instance




                                                 4
How to implement a Singleton in PHP?




                                       5
How to use the Singleton?




                            6
So, it is really cool, isn‘t it?




                                   7
Singleton and PHP are no friends


     Singleton: unique instance for application



         PHP: Shared-Nothing architecture



     Dilemma: instance only unique for request




                                                  8
Singleton has even more problems


               Dependency hiding



       Globals are bad, singleton is a global



             No Unit Testing possible




                                                9
Is there a solution?




                       10
Solution: Just create one!


        Code Convention: just create one




           Trust your Team members!




                                           11
Solution: Dependency Injection

       Create one instance and „inject“ it


      Don‘t create objects in regular classes


        Objects should be loosely coupled



                „Ask, don‘t look“



                                                12
Solution: Dependency Injection




                                 13
Solution: Factory

            Encapsulates object creation




              Handles object lifetimes




      Different factories for different lifetimes



                                                    14
Solution: Factory




                    15
Solution: PHP application lifetime


     Cache or Session for „application“ lifetime




        Factory can handle Cache or Session




                                                   16
„Friends don‘t let friends do Singleton“




                                           17
Benefits!

               Unit Testing is possible!


              Better lifetime handling!


                Easy object creation!



            Easy to change dependencies!



                                           18
And about the good Singleton…




                                19
The only good Singleton…




                           20
Thank you!




             21
Bigpoint GmbH
         Frank Sons
Senior Software Engineer CTD

      Drehbahn 47-48
      20354 Hamburg
         Germany

   Tel +49 40.88 14 13 - 0
  Fax +49 40.88 14 13 - 11

     info@bigpoint.net
     www.bigpoint.net



    Find us on


                               23

More Related Content

Viewers also liked (19)

hormel foods 2004_Proxy
hormel foods  2004_Proxyhormel foods  2004_Proxy
hormel foods 2004_Proxy
 
hormel foods ALL1999
hormel foods  ALL1999hormel foods  ALL1999
hormel foods ALL1999
 
10 ways to_improve
10 ways to_improve10 ways to_improve
10 ways to_improve
 
Make-up
Make-upMake-up
Make-up
 
Pw Presentation 1
Pw Presentation 1Pw Presentation 1
Pw Presentation 1
 
Ellis Hope 2010
Ellis Hope 2010Ellis Hope 2010
Ellis Hope 2010
 
Nano Bio Art040201
Nano Bio Art040201Nano Bio Art040201
Nano Bio Art040201
 
1438 Misin Imposible I,Ii,Iii
1438 Misin Imposible I,Ii,Iii1438 Misin Imposible I,Ii,Iii
1438 Misin Imposible I,Ii,Iii
 
Un mundo de ternura
Un mundo de ternuraUn mundo de ternura
Un mundo de ternura
 
10 Leadership Reminders
10 Leadership Reminders10 Leadership Reminders
10 Leadership Reminders
 
O πατέρας και O γιός
O πατέρας και O γιόςO πατέρας και O γιός
O πατέρας και O γιός
 
Introducing Cds Power Point Show
Introducing Cds Power Point ShowIntroducing Cds Power Point Show
Introducing Cds Power Point Show
 
期中課堂報告 9479104
期中課堂報告 9479104期中課堂報告 9479104
期中課堂報告 9479104
 
Top Dog Portfolio
Top  Dog  PortfolioTop  Dog  Portfolio
Top Dog Portfolio
 
My pet
My petMy pet
My pet
 
Brannprosjektering 01 generelle krav til sikkerhet ved brann
Brannprosjektering 01 generelle krav til sikkerhet ved brannBrannprosjektering 01 generelle krav til sikkerhet ved brann
Brannprosjektering 01 generelle krav til sikkerhet ved brann
 
omnicare annual reports 2006
omnicare annual reports 2006omnicare annual reports 2006
omnicare annual reports 2006
 
Městská karta
Městská kartaMěstská karta
Městská karta
 
telephone data systems 2001AR
telephone data systems  2001ARtelephone data systems  2001AR
telephone data systems 2001AR
 

Similar to The Singleton Dilemma

An idea in a making
An idea in a makingAn idea in a making
An idea in a making
Lisa Tyboni
 
YOOX Launch & Learn - Javascript as a programming language
 YOOX Launch & Learn - Javascript as a programming language YOOX Launch & Learn - Javascript as a programming language
YOOX Launch & Learn - Javascript as a programming language
Marco Cedaro
 
Developing solid applications
Developing solid applicationsDeveloping solid applications
Developing solid applications
Nilesh Bangar
 
Chaining the Beast - Testing Wonder Applications in the Real World
Chaining the Beast - Testing Wonder Applications in the Real WorldChaining the Beast - Testing Wonder Applications in the Real World
Chaining the Beast - Testing Wonder Applications in the Real World
WO Community
 

Similar to The Singleton Dilemma (20)

Rick Blalock: Your Apps are Leaking - Controlling Memory Leaks
Rick Blalock: Your Apps are Leaking - Controlling Memory LeaksRick Blalock: Your Apps are Leaking - Controlling Memory Leaks
Rick Blalock: Your Apps are Leaking - Controlling Memory Leaks
 
Desenvolvimento moderno de aplicativos android
Desenvolvimento moderno de aplicativos androidDesenvolvimento moderno de aplicativos android
Desenvolvimento moderno de aplicativos android
 
An idea in a making
An idea in a makingAn idea in a making
An idea in a making
 
jsDay - Javascript as a programming language
jsDay - Javascript as a programming languagejsDay - Javascript as a programming language
jsDay - Javascript as a programming language
 
YOOX Launch & Learn - Javascript as a programming language
 YOOX Launch & Learn - Javascript as a programming language YOOX Launch & Learn - Javascript as a programming language
YOOX Launch & Learn - Javascript as a programming language
 
What can social software do for your business & isw ignition
What can social software do for your business & isw ignitionWhat can social software do for your business & isw ignition
What can social software do for your business & isw ignition
 
Connections ignition sept 2011 business value of social software & user ado...
Connections ignition sept 2011   business value of social software & user ado...Connections ignition sept 2011   business value of social software & user ado...
Connections ignition sept 2011 business value of social software & user ado...
 
Developing solid applications
Developing solid applicationsDeveloping solid applications
Developing solid applications
 
30% faster coder on-boarding when you have a code cookbook
30% faster coder on-boarding when you have a code cookbook30% faster coder on-boarding when you have a code cookbook
30% faster coder on-boarding when you have a code cookbook
 
Visión SenS - Why should blind people be limited to information in Braille ?
Visión SenS - Why should blind people be limited to information in Braille ?Visión SenS - Why should blind people be limited to information in Braille ?
Visión SenS - Why should blind people be limited to information in Braille ?
 
ISS Art. How to do IT. Kotlin Multiplatform
ISS Art. How to do IT. Kotlin MultiplatformISS Art. How to do IT. Kotlin Multiplatform
ISS Art. How to do IT. Kotlin Multiplatform
 
Pragmatic programmer 2
Pragmatic programmer 2Pragmatic programmer 2
Pragmatic programmer 2
 
Chaining the Beast - Testing Wonder Applications in the Real World
Chaining the Beast - Testing Wonder Applications in the Real WorldChaining the Beast - Testing Wonder Applications in the Real World
Chaining the Beast - Testing Wonder Applications in the Real World
 
NDC 2011 - SpecFlow: Pragmatic BDD for .NET
NDC 2011 - SpecFlow: Pragmatic BDD for .NETNDC 2011 - SpecFlow: Pragmatic BDD for .NET
NDC 2011 - SpecFlow: Pragmatic BDD for .NET
 
Nagios Conference 2012 - Nathan Vonnahme - Monitoring the User Experience
Nagios Conference 2012 - Nathan Vonnahme - Monitoring the User ExperienceNagios Conference 2012 - Nathan Vonnahme - Monitoring the User Experience
Nagios Conference 2012 - Nathan Vonnahme - Monitoring the User Experience
 
13 Tips for Getting the Most Out of Your Next Hackathon
13 Tips for Getting the Most Out of Your Next Hackathon13 Tips for Getting the Most Out of Your Next Hackathon
13 Tips for Getting the Most Out of Your Next Hackathon
 
10 Big Ideas from Industry
10 Big Ideas from Industry10 Big Ideas from Industry
10 Big Ideas from Industry
 
Systematic Inventive Thinking - SIT
Systematic Inventive Thinking - SITSystematic Inventive Thinking - SIT
Systematic Inventive Thinking - SIT
 
Ten Lessons in Designing Content for Mobile
Ten Lessons in Designing Content for MobileTen Lessons in Designing Content for Mobile
Ten Lessons in Designing Content for Mobile
 
Dark Matter, Public Health, and Scientific Computing
Dark Matter, Public Health, and Scientific ComputingDark Matter, Public Health, and Scientific Computing
Dark Matter, Public Health, and Scientific Computing
 

More from Frank Sons

More from Frank Sons (20)

An Introduction to pair programming methods
An Introduction to pair programming methodsAn Introduction to pair programming methods
An Introduction to pair programming methods
 
Introduction to architecture katas
Introduction to architecture katasIntroduction to architecture katas
Introduction to architecture katas
 
Pair programming
Pair programmingPair programming
Pair programming
 
Effective Code Reviews (IPC 2018)
Effective Code Reviews (IPC 2018)Effective Code Reviews (IPC 2018)
Effective Code Reviews (IPC 2018)
 
Architecture Katas - How can we learn to create better architectures?
Architecture Katas - How can we learn to create better architectures?Architecture Katas - How can we learn to create better architectures?
Architecture Katas - How can we learn to create better architectures?
 
Architektur Kata - Workshop Solutions
Architektur Kata - Workshop SolutionsArchitektur Kata - Workshop Solutions
Architektur Kata - Workshop Solutions
 
Effektive Code Reviews Solutions
Effektive Code Reviews SolutionsEffektive Code Reviews Solutions
Effektive Code Reviews Solutions
 
Creating a good architecture
Creating a good architectureCreating a good architecture
Creating a good architecture
 
Effective Code Reviews
Effective Code ReviewsEffective Code Reviews
Effective Code Reviews
 
"Effektive Code Reviews" - Abendvortrag bei oose.de
"Effektive Code Reviews" - Abendvortrag bei oose.de"Effektive Code Reviews" - Abendvortrag bei oose.de
"Effektive Code Reviews" - Abendvortrag bei oose.de
 
Code Qualität in agilen Teams - code.talks Hamburg 2015
Code Qualität in agilen Teams - code.talks Hamburg 2015Code Qualität in agilen Teams - code.talks Hamburg 2015
Code Qualität in agilen Teams - code.talks Hamburg 2015
 
Imposter Syndrom - Lightning talk PHP Unconference 2015
Imposter Syndrom - Lightning talk PHP Unconference 2015Imposter Syndrom - Lightning talk PHP Unconference 2015
Imposter Syndrom - Lightning talk PHP Unconference 2015
 
Code Quality in an agile world - PHP Usergroup Hamburg
Code Quality in an agile world - PHP Usergroup HamburgCode Quality in an agile world - PHP Usergroup Hamburg
Code Quality in an agile world - PHP Usergroup Hamburg
 
Code Qualität in agilen Teams - Webmontag Hamburg
Code Qualität in agilen Teams - Webmontag HamburgCode Qualität in agilen Teams - Webmontag Hamburg
Code Qualität in agilen Teams - Webmontag Hamburg
 
Das Hochstapler Syndrom
Das Hochstapler SyndromDas Hochstapler Syndrom
Das Hochstapler Syndrom
 
Code reviews
Code reviewsCode reviews
Code reviews
 
Was bringen Code Reviews wirklich? Wir sind doch schon agil.
Was bringen Code Reviews wirklich? Wir sind doch schon agil.Was bringen Code Reviews wirklich? Wir sind doch schon agil.
Was bringen Code Reviews wirklich? Wir sind doch schon agil.
 
Code Review
Code ReviewCode Review
Code Review
 
Presenting to win - how I changed some old slides
Presenting to win - how I changed some old slidesPresenting to win - how I changed some old slides
Presenting to win - how I changed some old slides
 
Selbstmotivation - vom Intro zum Extro
Selbstmotivation - vom Intro zum ExtroSelbstmotivation - vom Intro zum Extro
Selbstmotivation - vom Intro zum Extro
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

The Singleton Dilemma