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    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 353 All time: 6,856 This week: 312Up
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: 6 packages by
Country: Bulgaria Bulgaria
Age: 34
All time rank: 265918 in Bulgaria Bulgaria
Week rank: 644 Up2 in Bulgaria Bulgaria Up
Innovation award
Innovation award
Nominee: 5x

  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:353
This week:0
All time:6,856
This week:312Up
User Comments (6)
There's a typo in all the files.
5 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
5 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
5 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
5 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
5 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
5 years ago (pvdptje)
47%StarStarStar
 

For more information send a message to info at phpclasses dot org.