PHP Classes

File: Falcraft/Patterns/SingletonExampleAndTest.php

Recommend this page to a friend!
  Classes of Asher Wolfstein   Singleton Trait   Falcraft/Patterns/SingletonExampleAndTest.php   Download  
File: Falcraft/Patterns/SingletonExampleAndTest.php
Role: Example script
Content type: text/plain
Description: A registry singleton example
Class: Singleton Trait
Trait that adds singleton pattern to a class
Author: By
Last change: Improved Usage and Testing
Date: 9 years ago
Size: 3,840 bytes
 

Contents

Class file image Download
<?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";