PHP Classes

File: singletone_examples.php

Recommend this page to a friend!
  Classes of Kiril Savchev   PHP Singleton Trait   singletone_examples.php   Download  
File: singletone_examples.php
Role: Example script
Content type: text/plain
Description: Examples with SIngletone trait
Class: PHP Singleton Trait
Implement the Singleton pattern as a trait
Author: By
Last change: Correct the path to required files
Date: 9 years ago
Size: 656 bytes
 

Contents

Class file image Download
<?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());
}

?>