PHP Classes

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: 360 This week: 1All time: 6,908 This week: 560Up
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: 38
All time rank: 266718 in Bulgaria Bulgaria
Week rank: 106 Up1 in Bulgaria Bulgaria Up
Innovation award
Innovation award
Nominee: 5x

Example

<?php

require_once 'ite/structs/Singletone.php';

use
ite\structs\Singletone;

class
MySingletoneClass {

    use
Singletone;

    public function
myMethod() {
        return
'Singletone trait example';
    }

}

try {
   
$MySingletoneObject = MySingletoneClass::getInstance();
    echo
$MySingletoneObject->myMethod();

   
$object = $MySingletoneObject();
   
var_dump($object === $MySingletoneObject);

   
$str = serialize($MySingletoneObject);
}
catch (
RuntimeException $e) {
    try {
       
$object = clone $MySingletoneObject
   
}
    catch (
RuntimeException $ee) {
        die(
$e->getMessage().'<br />'.$ee->getMessage());
    }
    die(
$e->getMessage());
}

?>


  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:360
This week:1
All time:6,908
This week:560Up
User Comments (6)
There's a typo in all the files.
8 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
8 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
8 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
8 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
8 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
8 years ago (pvdptje)
47%StarStarStar