baxtian/php-singleton 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

baxtian/php-singleton

最新稳定版本:0.6.6

Composer 安装命令:

composer require baxtian/php-singleton

包简介

PHP Trait Singleton

README 文档

README

This is a php trait for Singleton pattern and dependencies injection.

Stable Release: 0.1.0

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
{
	...
	...
	...
}

Mantainers

Juan Sebastián Echeverry baxtian.echeverry@gmail.com

Changelog

0.6.6

  • Added information to use in IDE
  • Added information to use with phpdoc in IDE

0.6.1

  • Use get_dependency as dependency constructor where available.

0.6

  • Add direct injection methods.

0.5

  • In case of using this class in multiple providers, allow Composer to set which file to use by default.

0.4.1

  • Using recomendations for wakeup and clone.

0.4

  • Using PSR naming recomendation.

0.3

  • Bugs fixed

0.2

  • Allow to use PHP8.0

0.1

  • First stable release

统计信息

  • 总下载量: 1.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2020-02-21