当前位置:首页 > 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');

实现容器的单例模式

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

php容器实现

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实现图片上传

实现图片上传的基本步骤 创建HTML表单用于文件上传,表单需设置enctype="multipart/form-data"属性,确保文件数据正确传输。 <form action="upload…

php实现上传

php实现上传

PHP 文件上传实现 HTML 表单设置 创建一个包含文件上传字段的 HTML 表单,确保表单的 enctype 属性设置为 multipart/form-data,这是文件上传必需的设置。 <…

php验证码实现

php验证码实现

验证码实现方法 在PHP中实现验证码功能通常涉及生成随机字符串或数字,将其转换为图像,并通过会话(Session)进行验证。以下是几种常见的实现方式: 使用GD库生成图像验证码 GD库是PHP内置的…

php 购物车实现

php 购物车实现

数据库设计 购物车功能通常需要设计数据库表存储商品和用户信息。常见的表包括products(商品表)、users(用户表)和cart(购物车表)。cart表通常包含字段:id(主键)、user_id(…

php实现重定向

php实现重定向

使用header函数进行重定向 在PHP中,header()函数是实现重定向的常用方法。通过发送HTTP头信息Location,浏览器会自动跳转到指定URL。需确保在调用header()前没有输出任何…

php 实现https

php 实现https

在 PHP 中实现 HTTPS 确保 PHP 应用通过 HTTPS 协议安全传输数据,可以通过以下几种方式实现: 配置服务器强制 HTTPS 在 Web 服务器(如 Apache 或 Nginx)中…