当前位置:首页 > PHP

php容器实现

2026-02-28 10:32:46PHP

PHP 容器实现

在 PHP 中,容器(Container)通常指依赖注入容器(Dependency Injection Container),用于管理对象的创建和依赖关系。以下是实现 PHP 容器的几种常见方法:

使用数组实现简单容器

通过数组存储服务定义和实例,实现基本的容器功能。

class Container {
    protected $services = [];

    public function set($name, callable $definition) {
        $this->services[$name] = $definition;
    }

    public function get($name) {
        if (!isset($this->services[$name])) {
            throw new Exception("Service not found: {$name}");
        }
        return $this->services[$name]($this);
    }
}

使用反射实现自动依赖注入

通过 PHP 的 Reflection API 自动解析类的依赖关系并实例化对象。

class Container {
    protected $instances = [];

    public function get($class) {
        if (isset($this->instances[$class])) {
            return $this->instances[$class];
        }

        $reflector = new ReflectionClass($class);
        $constructor = $reflector->getConstructor();

        if (!$constructor) {
            return new $class;
        }

        $parameters = $constructor->getParameters();
        $dependencies = [];

        foreach ($parameters as $parameter) {
            $dependency = $parameter->getClass();
            if ($dependency) {
                $dependencies[] = $this->get($dependency->name);
            } else {
                throw new Exception("Cannot resolve dependency: {$parameter->name}");
            }
        }

        $this->instances[$class] = $reflector->newInstanceArgs($dependencies);
        return $this->instances[$class];
    }
}

使用 PSR-11 标准实现容器

遵循 PSR-11(Container Interface)标准实现容器,提高与其他框架的兼容性。

use Psr\Container\ContainerInterface;

class Container implements ContainerInterface {
    protected $services = [];

    public function get($id) {
        if (!$this->has($id)) {
            throw new ContainerException("Service not found: {$id}");
        }
        return $this->services[$id]($this);
    }

    public function has($id) {
        return isset($this->services[$id]);
    }

    public function set($id, callable $definition) {
        $this->services[$id] = $definition;
    }
}

使用现有容器库

直接使用成熟的 PHP 容器库,如 PHP-DI、Laravel 的容器等。

// 使用 PHP-DI
$container = new DI\Container();
$container->set('service', function() {
    return new SomeService();
});
$service = $container->get('service');

实现容器的单例模式

确保某些服务在容器中以单例模式存在,避免重复创建。

class Container {
    protected $singletons = [];

    public function singleton($name, callable $definition) {
        $this->singletons[$name] = $definition;
    }

    public function get($name) {
        if (isset($this->singletons[$name])) {
            if (!isset($this->instances[$name])) {
                $this->instances[$name] = $this->singletons[$name]($this);
            }
            return $this->instances[$name];
        }
        throw new Exception("Service not found: {$name}");
    }
}

通过以上方法,可以实现不同复杂度的 PHP 容器,满足项目需求。

php容器实现

标签: 容器php
分享给朋友:

相关文章

php 实现接口

php 实现接口

PHP 实现接口的方法 在 PHP 中,接口(Interface)用于定义一组方法的规范,而不包含具体的实现。类可以实现(implement)接口,并必须实现接口中定义的所有方法。 定义接口 接口…

php队列的实现

php队列的实现

PHP队列的实现方式 PHP中实现队列功能可以通过多种方式,根据需求选择适合的方案: 使用数组实现基础队列 PHP数组本身支持队列操作,利用array_push和array_shift函数: $q…

php 实现类

php 实现类

PHP 类的基本实现 在 PHP 中,类通过 class 关键字定义,后跟类名和一对花括号。类名通常遵循大驼峰命名法(如 ClassName)。以下是基本语法: class ClassName {…

php实现的进度条

php实现的进度条

PHP 实现的进度条方法 在 PHP 中实现进度条通常可以通过命令行脚本(CLI)或 Web 页面两种方式完成。以下是几种常见的实现方法: 命令行进度条 使用 PHP CLI 实现进度条,可以通过不…

php实现支付宝支付

php实现支付宝支付

支付宝支付集成步骤 准备工作 在开始集成支付宝支付前,确保已注册支付宝开放平台账号并完成企业实名认证。创建应用后获取APPID、应用私钥和支付宝公钥。 配置支付宝SDK 通过Composer安装支付…

php 实现链表

php 实现链表

PHP 实现链表的方法 链表是一种线性数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。PHP 中可以通过类和对象来实现链表。 定义链表节点类 创建一个 ListNode 类,用于表示…