The Singleton Dilemma
Upcoming SlideShare
Loading in...5

The Singleton Dilemma



Presentation I held internally at Bigpoint about what the main problem with Singleton, especially in PHP, is.

Presentation I held internally at Bigpoint about what the main problem with Singleton, especially in PHP, is.



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

The Singleton Dilemma The Singleton Dilemma Presentation Transcript

  • The Singleton DilemmaFrank SonsSenior 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 SonsSenior Software Engineer CTD Drehbahn 47-48 20354 Hamburg Germany Tel +49 40.88 14 13 - 0 Fax +49 40.88 14 13 - 11 Find us on 23