Search by

baxtian / php-singleton

baxtian

PHP Trait Singleton

Package info

bitbucket.org/baxtian/php-singleton

pkg:composer/baxtian/php-singleton

Statistics

Installs: 1 504

Dependents: 6

Suggesters: 0

0.6.9 2026-09-19 12:43 UTC

This package is auto-updated.

Last update: 2026-09-19 12:43:30 UTC


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();
  1. Repeat use Baxtian\SingletonTrait; in the subclass. The trait declares private static $instance — in PHP, a private property declared inside a trait belongs to whichever class first uses the trait, not to every subclass automatically. Skip this and YourSubclass::get_instance() silently returns an instance of YourClass (the parent) instead of YourSubclass — no error, the subclass's own behavior just never runs.

  2. Declare your own __construct() that calls parent::__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 of YourClass::__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.phpmerak-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.