Component\Util\Singleton is a simple-to-use PHP component that provides the Singleton (and Registry of Singletons a.k.a. Multiton) creational design pattern.
Singleton is a creational design pattern and defined as follows:
"Ensure a class only has once instance, and provide a global access point to it."
-- E. Gamma, et al. Design Patterns: Elements of Reusable Object-Oriented software. Westford: Addison-Wesley, 1995.
Component\Util\Singleton contains of two traits:
- The trait
SingletonTrait
: A generic implementation of the Singleton creational design pattern. - The trait
MultitonTrait
: A generic implementation of the Registry of Singletons a.k.a. Multiton creational design pattern.
The generic implementations as traits make this component reusable (see the section Features below).
- Allows to pass parameters to the static Creation Method
getInstance()
. The parameters are passed to theprotected
constructor of the class using the component. - Allows a number of unlimited classes to use the component.
- A class which uses the
SingletonTrait
(orMultitonTrait
) can still inherit from another class. - A class which extends a class using the
SingletonTrait
(orMultitonTrait
) is a new Singleton (or Multiton). Therefore the component supports inheritance.
- A class which uses the
- Disallows cloning and serializing of a Singleton (or Multiton) instance.
- Disallows overriding of the methods
getInstance()
,__clone
and__wakeup
. - Follows the naming conventions for the Singleton design pattern (offers a
static Creation Method
getInstance()
). - Artifacts tested with both static and dynamic test procedures:
- Dynamic component tests (unit and integration tests) implemented with PHPUnit.
- Static code analysis performed using the following tools:
- PHP_CodeSniffer: Style Checker
- PHP Mess Detector (PHPMD): Code Analyzer
- PHP Depend: Code Metrics
- phpcpd: Copy/Paste Detector (CPD)
- phpdcd: Dead Code Detector (DCD)
- SensioLabs Security Checker: Security Checker
- Continuous Integration (CI) using the following web services:
- Provides a Packagist package which can be installed using the dependency manager Composer. Click here for the package on Packagist.
- Provides a complete Application Programming Interface (API) documentation generated with the documentation generator Sami. Click here for the API documentation.
- Follows the following "standards" from the PHP Framework Interoperability
Group (FIG). PSR stands for PHP Standards Recommendation:
-
PSR-0: Autoloading Standards
Aims to provide a standard file, class and namespace convention to allow plug-and-play code.
-
PSR-1: Basic Coding Standard
Aims to ensure a high level of technical interoperability between shared PHP code.
-
PSR-2: Coding Style Guide
Provides a Coding Style Guide for projects looking to standardize their code.
-
PSR-4: Autoloader
A more modern take on autoloading reflecting advances in the ecosystem.
-
- Follows the Semantic Versioning (SemVer) specification version 2.0.0.
- PHP >= 5.4
- Composer
- florianwolters/component-util-reflection
- florianwolters/component-core-stringutils
- PHPUnit
- phpcpd
- phpdcd
- PHP_CodeSniffer
- PHP Mess Detector (PHPMD)
- Sami
- SensioLabs Security Checker
- php-coveralls
Component\Util\Singleton should be installed using the dependency manager Composer.
Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.
The Composer installer can be downloaded with php
.
php -r "readfile('https://getcomposer.org/installer');" | php
This will just check a few PHP settings and then download
composer.phar
to your working directory. This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP which can be run on the command line, amongst other things.
To resolve and download dependencies, run the
install
command:
php composer.phar install
If you are creating a component that relies on Component\Util\Singleton,
please make sure that you add Component\Util\Singleton to your component's
composer.json
file:
{
"require": {
"florianwolters/component-util-singleton": "0.3.*"
}
}
The best documentation for Component\Util\Singleton are the unit tests, which are shipped in the package.
Additional documentation can be found in the official Wiki of the project.
phpunit
Please see CONTRIBUTING for details.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see https://gnu.org/licenses/lgpl.txt.