承接 phpnomad/singleton 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

phpnomad/singleton

最新稳定版本:1.0.1

Composer 安装命令:

composer require phpnomad/singleton

包简介

README 文档

README

Latest Version Total Downloads PHP Version License

phpnomad/singleton is a single-trait package for PHPNomad. It provides WithInstance, a trait that gives any class a static instance() method returning a lazily-created, per-class singleton. It has zero runtime dependencies and is what phpnomad/facade and the rest of the framework use whenever a class needs a single shared instance without reaching for a container.

Installation

composer require phpnomad/singleton

Quick Start

Add the trait to a class, then call instance() to get the shared object.

<?php

use PHPNomad\Singleton\Traits\WithInstance;

class Registry
{
    use WithInstance;

    private array $items = [];

    public function register(string $key, $value): void
    {
        $this->items[$key] = $value;
    }

    public function get(string $key)
    {
        return $this->items[$key] ?? null;
    }
}

Registry::instance()->register('theme', 'dark');
$theme = Registry::instance()->get('theme'); // 'dark'

Every call to Registry::instance() returns the same object for the lifetime of the request.

Overview

The package is intentionally tiny. The trait adds a protected static $instance property and a public static instance() method, and nothing else.

  • Lazy initialization, so the instance is only built the first time instance() is called
  • Late static binding (new static), so subclasses each get their own singleton rather than sharing the parent's
  • Zero runtime dependencies, which means adding it to a project pulls in nothing else
  • Lives under the PHPNomad\Singleton\Traits namespace, importable via use PHPNomad\Singleton\Traits\WithInstance;
  • Used by phpnomad/facade to back its static service facades across the framework

Documentation

The full package guide, including inheritance behavior, testing strategies, and when to reach for a DI container instead, lives at phpnomad.com.

License

MIT, see LICENSE.txt for the full text.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固