PHP Classes

Arsenal PHP Singleton Trait: Trait to make classes only have singleton objects

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 yet rated by the usersTotal: 145 All time: 9,143 This week: 73Up
Version License PHP version Categories
arsenal 1.1Freeware7Language, Design Patterns, PHP 7, Traits
Description 

Author

This package provides a trait to make classes only have singleton objects.

The trait provides a getter function that initializes an object on the first time a variable of the class is accessed with an instance of the target object that will act as a singleton object for the class.

A setter function will assign the variables of the target object that acts as singleton object for that class.

A constant array can be used to define how to map classes to target classes that will have objects acting as singletons.

Innovation Award
PHP Programming Innovation award nominee
January 2020
Number 4
Singleton is a common design pattern used by many developers to assure that only one object of a class is created even if the software tries to create objects of that class more than once.

This package provides an alternative approach to assure that only one object of a class is created by using PHP constant values to keep track of the objects that are created.

Manuel Lemos
Picture of wim niemans
  Performance   Level  
Name: wim niemans <contact>
Classes: 6 packages by
Country: The Netherlands The Netherlands
Age: ???
All time rank: 345989 in The Netherlands The Netherlands
Week rank: 180 Up6 in The Netherlands The Netherlands Up
Innovation award
Innovation award
Nominee: 2x

Example

<?php

include_once 'arsenal.php';

Class
A
{
use
arsenal;
function
__construct() { echo 'Creating class A as ' . __CLASS__ . " \n"; }
function
sayHello() { echo 'Hello from class ' . __CLASS__ . " \n"; }
}
Class
B
{
use
arsenal;
function
__construct() { echo 'Creating class B as ' . __CLASS__ . " \n"; }
function
sayHello() { echo 'Hello from class ' . __CLASS__ . " \n"; }
}

Class
A1
{
function
__construct($obj) { echo 'Creating class A1 from ' . get_class($obj) . " \n"; }
function
sayHello() { echo 'Hello from class ' . __CLASS__ . " \n"; }
}
Class
A2
{
function
__construct($obj) { echo 'Creating class A2 from ' . get_class($obj) . " \n"; }
function
sayHello() { echo 'Hello from class ' . __CLASS__ . " \n"; }
}
Class
B1
{
function
__construct($obj) { echo 'Creating class B1 from ' . get_class($obj) . " \n"; }
function
sayHello() { echo 'Hello from class ' . __CLASS__ . " \n"; }
}
Class
B2
{
function
__construct($obj) { echo 'Creating class B2 from ' . get_class($obj) . " \n"; }
function
sayHello() { echo 'Hello from class ' . __CLASS__ . " \n"; }
}

$a = new A;
$b = new B;
print_r($a);
print_r($b);

$a->a1->sayHello();
$a->a2->sayHello();
$a->b1->sayHello();
$a->b2->sayHello();
$b->b1->sayHello();
$b->b2->sayHello();

print_r($a);
print_r($b);

$a->a1->sayHello();
$a->a2->sayHello();
$a->b1->sayHello();
$a->b2->sayHello();
$b->b1->sayHello();
$b->b2->sayHello();

$b->a1->sayHello(); // throws error !!

/* Ouputs:
Creating class A as A
Creating class B as B
A Object
(
)
B Object
(
)
Creating class A1 from A
Hello from class A1
Creating class A2 from A
Hello from class A2
Creating class B1 from A
Hello from class B1
Creating class B2 from A
Hello from class B2
Creating class B1 from B
Hello from class B1
Creating class B2 from B
Hello from class B2
A Object
(
    [a1] => A1 Object
        (
        )

    [a2] => A2 Object
        (
        )

    [b1] => B1 Object
        (
        )

    [b2] => B2 Object
        (
        )

)
B Object
(
    [b1] => B1 Object
        (
        )

    [b2] => B2 Object
        (
        )

)
Hello from class A1
Hello from class A2
Hello from class B1
Hello from class B2
Hello from class B1
Hello from class B2

Fatal error: Uncaught Error: Call to a member function sayHello() on null in _test/demo.php:61
Stack trace:
#0 {main}
  thrown in _test/demo.php on line 61
 */
 
?>


  Files folder image Files (2)  
File Role Description
Plain text file arsenal.php Class singleton factory trait
Accessible without login Plain text file demo.php Example example script

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:145
This week:0
All time:9,143
This week:73Up