PHP Classes

Singleton Trait: Trait that adds singleton pattern to a class

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: 250 All time: 7,925 This week: 49Up
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  
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 (15)  
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

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 Reuses Unique User Downloads Download Rankings  
 0%1
Total:250
This week:0
All time:7,925
This week:49Up