PHP Classes

Singleton Trait: Trait that adds singleton pattern to a class

Recommend this page to a friend!
  Info   View files Example   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 249 This week: 1All time: 7,928 This week: 560Up
Version License PHP version Categories
singleton-trait 3.2MIT/X Consortium ...5.4PHP 5, Language, Design Patterns, Traits
Description 

Author

This is a trait that adds singleton pattern to a class.

The trait can allow the creation of the first object of the class taking optional configuration parameters.

Classes that use the trait may override the init() method to process the parameters that may be restricted to the first instantiation of the object.

Picture of Asher Wolfstein
  Performance   Level  
Name: Asher Wolfstein <contact>
Classes: 15 packages by
Country: United States United States
Age: 40
All time rank: 1122164 in United States United States
Week rank: 52 Up6 in United States United States Up
Innovation award
Innovation award
Nominee: 5x

Example

<?php

require_once('SingletonTrait.php');

use
Falcraft\Patterns;

class
TestSingleton {
    use
Patterns\SingletonTrait;
   
    public
$publicprop;
    private
$privateprop;
    public static
$staticProp = null;
   
    public function
init($testarg1, $testarg2)
    {
       
$this->publicProp = $testarg1;
       
$this->privateProp = $testarg2;
       
self::$staticProp = 5;
    }
   
    public function
getPrivateProp()
    {
        return
$this->privateProp;
    }
   
    public function
setStaticProp($staticarg)
    {
       
self::$staticprop = $staticarg;
    }
}

echo
"Falcraft\\Patterns\\SingletonTrait.php Test\n";
echo
"------------------------------------------\n\n";

echo
"Instantiating Singleton with Arguments -> ";

$success = true;

$testSingletonInstance = null;

try {
   
$testSingletonInstance = TestSingleton::instantiate(1, 2);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Check Hardened Through Static Call -> ";

$success = true;

$hardened = false;

try {
   
$hardened = TestSingleton::hardened();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$hardened == false) {
   
$success = false;
}

if (
$success) {
    echo
"Success: " . ( $hardened ? "Hardened\n" : "Not Hardened\n" );
} else {
    echo
"Failure...: " . ( $hardened ? "Hardened\n" : "Not Hardened\n" );
}

echo
"Check Hardened Through Member Function -> ";

$success = true;

try {
   
$hardened = $testSingletonInstance->hardened();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$hardened == false) {
   
$success = false;
}

if (
$success) {
    echo
"Success: " . ( $hardened ? "Hardened\n" : "Not Hardened\n" );
} else {
    echo
"Failure...: " . ( $hardened ? "Hardened\n" : "Not Hardened\n" );
}

echo
"Instantiate Again Into Alternate Local Variable -> ";

$success = true;

$testSingletonInstanceTwo = null;

try {
   
$testSingletonInstanceTwo = TestSingleton::instantiate();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Instantiate Into Another Local Variable with Arguments -> ";

$success = false;

try {
   
$testSingletonHardenedTest = TestSingleton::instantiate(4, 5);
} catch (\
Exception $e) {
   
$success = true;
}

if (
$success) {
    echo
"Success: Exception Raised\n";
} else {
    echo
"Failure...: No Exception Raised\n";
}

echo
"Accessing Private Attribute With Member Function -> ";

$success = true;

$privateProp = null;
$privateProp2 = null;

try {
   
$privateProp = $testSingletonInstance->getPrivateProp();
   
$privateProp2 = $testSingletonInstanceTwo->getPrivateProp();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success: $privateProp, $privateProp2\n";
} else {
    echo
"Failure...\n";
}

echo
"Accessing Public Attribute Without Member Function -> ";

$success = true;

$publicProp = null;
$publicProp2 = null;

try {
   
$publicProp = $testSingletonInstance->publicProp;
   
$publicProp2 = $testSingletonInstanceTwo->publicProp;
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success: $publicProp, $publicProp2\n";
} else {
    echo
"Failure...\n";
}

echo
"Setting And Accessing Public Static Singleton Attribute -> ";

$success = true;

$staticProp = null;

try {
   
TestSingleton::$staticProp = 9;
    if (
TestSingleton::$staticProp == 9) {
       
$success = true;
    } else {
       
$success = false;
    }
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"\n";


  Files folder image Files  
File Role Description
Files folder imageFalcraft (1 directory)
Files folder imagesrc (1 directory)
Files folder imagetests (1 directory)
Accessible without login Plain text file composer.json Conf. Composer File
Accessible without login Plain text file LICENSE.txt Lic. License (MIT)
Accessible without login Plain text file phpunit.xml Test Unit Testing
Accessible without login Plain text file README.txt Doc. README

  Files folder image Files  /  Falcraft  
File Role Description
Files folder imagePatterns (2 files, 2 directories)

  Files folder image Files  /  Falcraft  /  Patterns  
File Role Description
Files folder imageException (2 files)
Files folder imageResource (1 file)
  Accessible without login Plain text file SingletonExampleAndTest.php Example A registry singleton example
  Plain text file SingletonTrait.php Class The Singleton Trait

  Files folder image Files  /  Falcraft  /  Patterns  /  Exception  
File Role Description
  Plain text file ExceptionInterface.php Class Custom/Local Exception Interface
  Plain text file RuntimeException.php Class Custom/Local Runtime Exception

  Files folder image Files  /  Falcraft  /  Patterns  /  Resource  
File Role Description
  Plain text file SingletonInterface.php Class An interface to identify singletons, separate from trait

  Files folder image Files  /  src  
File Role Description
Files folder imagePhabstractic (1 file, 1 directory)

  Files folder image Files  /  src  /  Phabstractic  
File Role Description
Files folder imagePatterns (2 directories)
  Accessible without login Plain text file falcraftLoad.php Conf. Loader

  Files folder image Files  /  src  /  Phabstractic  /  Patterns  
File Role Description
Files folder imageException (2 files)
Files folder imageResource (2 files)

  Files folder image Files  /  src  /  Phabstractic  /  Patterns  /  Exception  
File Role Description
  Plain text file ExceptionInterface.php Class exception classification
  Plain text file RuntimeException.php Class exception classification

  Files folder image Files  /  src  /  Phabstractic  /  Patterns  /  Resource  
File Role Description
  Plain text file SingletonInterface.php Class Main Trait
  Plain text file SingletonTrait.php Class Main Trait

  Files folder image Files  /  tests  
File Role Description
Files folder imagePhabstractic (1 directory)

  Files folder image Files  /  tests  /  Phabstractic  
File Role Description
Files folder imagePatterns (1 directory)

  Files folder image Files  /  tests  /  Phabstractic  /  Patterns  
File Role Description
Files folder imageResource (1 file)

  Files folder image Files  /  tests  /  Phabstractic  /  Patterns  /  Resource  
File Role Description
  Accessible without login Plain text file SingletonTrait.php Test unit testing

 Version Control Reuses Unique User Downloads Download Rankings  
 0%1
Total:249
This week:1
All time:7,928
This week:560Up