atekushi/singleton
最新稳定版本:1.0.0
Composer 安装命令:
composer require atekushi/singleton
包简介
Small Library to Implement Singleton Pattern To Your Project
README 文档
README
The Singleton pattern is a design pattern that restricts the instantiation of a class to a single instance. It is commonly used to represent shared resources, such as configuration settings, logging services, or database connections.
Why Use it ?
- Ensures only one instance of a class is created.
- Supports lazy initialization (instance is created only when needed).
- Simple and easy-to-use API.
- Provides a utility method to check if a class follows the Singleton pattern.
Installation
Install the library via Composer:
composer require atekushi/singleton
Usage
Just extend the singleton class to your target class
include 'vendor/autoload.php'; use Atekushi\Singleton\Singleton; class Config extends Singleton { private array $settings; protected function __construct(array $settings = []) { $this->settings = $settings; } public function get(string $key, $default = null) { return $this->settings[$key] ?? $default; } } // Usage $config = Config::getInstance(['app_name' => 'MyApp', 'debug' => true]); echo $config->get('app_name');
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-14