PHP Classes

PHP Singleton Trait: Implement the Singleton pattern as a trait

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 360 All time: 6,911 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: 266518 in Bulgaria Bulgaria
Week rank: 192 Up2 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 (2)  
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 (2)  /  ite  
File Role Description
Files folder imagestructs (1 file)

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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:360
This week:0
All time:6,911
This week:560Up
User Comments (6)
There's a typo in all the files.
9 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
9 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
9 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
9 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
9 years ago (pvdptje)
47%StarStarStar
There's a typo in all the files.
9 years ago (pvdptje)
47%StarStarStar