baxtian / php-singleton
PHP Trait Singleton
Requires
- php: >=7.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
This is a php trait for Singleton pattern and dependencies injection.
Stable Release: 0.6.9
Usage:
<?php
class YourClass
{
use Baxtian\SingletonTrait;
/*
code of your class
*/
}
$obj = YourClass::get_instance();
For direct injection
<?php
// Dependencies to be used
class ClassA {
function echo() {
echo "A";
};
}
class ClassB {
function echo() {
echo "B";
};
}
class YourClass
{
use Baxtian\SingletonTrait;
// Create class constructor with a variable that will
// contain -if any- the list of arguments
public function __construct($arguments = [])
{
// Array of attributes linked to the class
$classes = [
'class_a' => ClassA::class,
'class_b' => ClassB::class,
];
$this->set_dependencies($arguments, $classes);
}
// To use direct call without using the method
public function __get($name) {
switch($name) {
case 'class_a':
case 'class_b':
return $this->dependency($name);
break;
}
return null;
}
// Function that uses an injected dependency
public function foo() {
$this->dependency('class_a')->echo();
$this->class_b->echo();
}
}
To pass a specific instance (as a mock for testing)
$foo = YourClass::get_instance([
'class_a' => new ClassA(),
'class_b' => new ClassB()
]);
To recognize the class type of the instance in the IDE add the @property-read attribute in the class documentation.
/**
* @property-read ClassA $class_a
* @property-read ClassB $class_b
*/
class YourClass
{
...
...
...
}
Subclassing
Extending a class that already uses SingletonTrait — same pattern baxtian/wp_p2p/wp_json/wp_settings/wp_jwt use for "extend and configure" — needs two things in the subclass, both silent if skipped (no error either way):
<?php
class YourSubclass extends YourClass
{
use Baxtian\SingletonTrait; // 1
protected function __construct() // 2
{
parent::__construct();
}
}
$obj = YourSubclass::get_instance();
Repeat
use Baxtian\SingletonTrait;in the subclass. The trait declaresprivate static $instance— in PHP, a private property declared inside a trait belongs to whichever class firstuses the trait, not to every subclass automatically. Skip this andYourSubclass::get_instance()silently returns an instance ofYourClass(the parent) instead ofYourSubclass— no error, the subclass's own behavior just never runs.Declare your own
__construct()that callsparent::__construct(), even with nothing else in it. Repeating the trait (step 1) also brings in its own empty__construct($arguments = [])— and a trait method takes precedence over an inherited one, even when the subclass never declares that method itself. Skip your own__construct()and the subclass silently runs the trait's empty stub instead ofYourClass::__construct()— whatever that constructor was supposed to set up (hooks, routes, state) never happens.
SingletonLibTrait
Same capabilities as SingletonTrait (lazy instance, constructor-argument injection), but under a
different importable name. Useful in Merak-based projects for "Libs" classes that need testable
dependency injection but must not be auto-registered in Instances.php — merak-cli's
composer reset detects Singleton usage via a literal string search for
use \Baxtian\SingletonTrait; in the project's src/ files, so importing this trait instead
opts a class out of that detection without losing any of the DI capabilities.
<?php
class YourLibClass
{
use Baxtian\SingletonLibTrait;
/*
code of your class
*/
}
$obj = YourLibClass::get_instance();
Mantainers
Juan Sebastián Echeverry baxtian.echeverry@gmail.com
Changelog
See CHANGELOG.md.