Main Content
Singleton design pattern - examples and uses Archive - Originally posted on "The Horse's Mouth" - 2016-01-20 16:43:04 - Graham Ellis
A Singleton class is one where you can only create one object of a type, or a second object is really a share of the first. An example in PHP of a Singleton design pattern - [here] . Uses a static (class) variable to flag object creation.
A singleton is especially sueful when you're looking to hold a quasi-global set of information or a shared database within an application, without wanting to actually have a global variable with all the namespace clutter that can bring. A further example from today's Object Oriented PHP course can be found [here] .
Singletons are great for such tasks as database connections, loading reference tables, etc ...
Some other articles
H108 - Objects in PHP Associative objects - one object within another. Caching results in an object for efficiency - avoiding re-calculation This article Changing what operators do on objects - a comparison across different programming languages Object factories in C++, Python, PHP and Perl Learning about Object Orientation in PHP - a new set of examples stdClass in PHP - using an object rather than an associative array Objects in PHP - Revision Caching Design Patterns Copying, duplicating, cloning an object in PHP Autoload in PHP How do classes relate to each other? Associated Classes Design Patterns - what are they? Why use them? Designing your application - using UML techniques Computer Graphics in PHP - World (incoming data) to Pixel (screen) conversion Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON Private and Public - and things between Getting the OO design write - with PHP a example Does copying a variable duplicate the contents? PHP - Object Oriented Design in use What is a factory? The Multiple Inheritance Conundrum, interfaces and mixins Static class members in PHP - a documented example Object Oriented Programming in PHP Shipping a test harness with your class in PHP Serialization - storing and reloading objects Abstract classes, Interfaces, PHP and Java Object Oriented programming - a practical design example PHP4 v PHP5 - Object Model Difference Cleaning up redundant objects When should I use OO techniques? PHP - getclass v instanceof Introduction to Object Oriented Programming Sorting objects in PHP Calling base class constructors Accounts in PHP - an OO demo OO PHP demonstration - comparing objects and more What are factory and singleton classes? Object Oriented Model - a summary of changes from PHP4 to PHP5 Cue the music, I'm happy. Build on what you already have with OO Planning a hotel refurb - an example of a Gant chart in PHP Think about your design even if you don't use full UML North, Norther and Northest - PHP 5 Objects Don't repeat code - use loops or functions Should I use structured or object oriented? PHP5 lets you say no PHP v Java Object Oriented Programming in PHP Q908 - Object Orientation: Design Patterns Separating detailed data code from the main application - Ruby example Easy data to object mapping (csv and Python) This article Thin application, thick objects - keep you main code simple. Example in Ruby How healthy are the stars of stage and screen? Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. Designing a base class and subclasses, and their extension, in C++ How to avoid too many recalculations within an object Object factories in C++, Python, PHP and Perl Java - factory method, encapsulation, hashmap example Learning to program - what are algorithms and design patterns? Using object orientation for non-physical objects Perl design patterns example Spike solution, refactored and reusable, Python - Example Caching Design Patterns Reading files, and using factories to create vectors of objects from the data in C++ Learning C++ - a design pattern for your first class Design Patterns - what are they? Why use them? What is a factory method and why use one? - Example in Ruby What is a factory? Looking for a practical standards course Object Relation Mapping (ORM) North, Norther and Northest - PHP 5 Objects