PHP Classes
elePHPant
Icontem

PHP Singleton Trait: Implement the Singleton pattern as a trait

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum (8)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2015-04-20 (1 year ago) RSS 2.0 feedNot enough user ratingsTotal: 333 All time: 6,749 This week: 894Up
Version License PHP version Categories
singletone 1.4GNU General Publi...5.4PHP 5, Language, Design Patterns, Traits
Description Author

This package can implement the Singleton pattern as a trait.

It defines a trait with a function named getinstance() that creates an instance of the current class if it does not exist.

The created object is stored in static variable of the trait, so next time it is checked, the created object is returned.

The trait defines the constructor as protected and the class using the trait can override it. The entire application can get the singleton object only instance via the public static method getInstance().

When there is an attempt to clone or serialize the object, the trait throws RuntimeException.

The static property for the only instance is declared as protected and is instantiated with the static keyword to allow the access from the extended class.

Innovation Award
PHP Programming Innovation award nominee
April 2015
Number 13
Many classes implement the singleton design pattern to assure that no more than one instance of a given class exists at a given moment.

This package implements the singleton pattern using a trait, so the it can be reused by many other packages that need to implement that same pattern.

Manuel Lemos
Picture of Kiril Savchev
  Performance   Level  
Name: Kiril Savchev <contact>
Classes: 5 packages by
Country: Bulgaria Bulgaria
Innovation award
Innovation award
Nominee: 4x

  Files folder image Files  
File Role Description
Files folder imageite (1 directory)
Accessible without login Plain text file singletone_examples.php Example Examples with SIngletone trait

  Files folder image Files  /  ite  
File Role Description
Files folder imagestructs (1 file)

  Files folder image Files  /  ite  /  structs  
File Role Description
  Plain text file Singletone.php Class Singletone trait

 Version Control Unique User Downloads Download Rankings  
 0%
Total:333
This week:0
All time:6,749
This week:894Up
User Comments (6)
There's a typo in all the files.
1 year ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
1 year ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
1 year ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
1 year ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
1 year ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
1 year ago (pvdptje)
47%StarStarStar