ioc-interop/interface
Composer 安装命令:
composer require ioc-interop/interface
包简介
Interoperable IOC container interfaces for PHP.
README 文档
README
Ioc-Interop provides an interoperable package of standard interfaces for inversion-of-control (IOC) service container functionality. It reflects, refines, and reconciles the common practices identified within several pre-existing projects.
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in BCP 14 (RFC 2119, RFC 8174).
This package attempts to adhere to the Package Development Standards approach to naming and versioning.
Interfaces
This package defines the following interfaces:
-
IocContainer affords obtaining services by name.
-
IocContainerFactory affords obtaining a new instance of IocContainer.
-
IocThrowable extends Throwable to mark an Exception as IOC-related.
-
IocTypeAliases provides custom PHPStan types to aid static analysis.
IocContainer
IocContainer affords obtaining services by name.
-
Notes:
- This interface does not afford service management. The container will need to obtain services somehow, e.g. from a Service-Interop implementation.
IocContainer Methods
-
public function hasService(ioc_service_name_string $serviceName) : bool;
-
Is the container able to return an instance of the
$serviceName? -
Notes:
- The logic for this method is expressly unspecified. The ability check may be accomplished by querying a service management subsystem, or by some other means.
-
-
public function getService( ioc_service_name_string $serviceName, ) : ioc_service_object;
-
Returns an instance of the
$serviceName. -
Directives:
- Implementations MUST throw IocThrowable if the container
cannot return an instance of the
$serviceName.
- Implementations MUST throw IocThrowable if the container
cannot return an instance of the
-
Notes:
-
The logic for this method is expressly unspecified. Retrieval may be accomplished via a service management subsystem, or by some other means.
-
The returned instance may be new or shared. The retrieval logic defines the service lifetime, not the container (per se) and not the caller requesting the service.
-
-
IocContainerFactory
IocContainerFactory affords obtaining a new instance of IocContainer.
IocContainerFactory Methods
-
public function newContainer() : IocContainer;
-
Returns a new instance of IocContainer.
-
Notes:
- Container instantiation logic is not specified. Implementations might use providers, configuration files, attribute or annotation collection, or some other means to create and populate a container. Implementations might also choose to return a compiled or otherwise reconstituted container.
-
IocThrowable
IocThrowable extends Throwable to mark an Exception as IOC-related.
It adds no class members.
IocTypeAliases
IocTypeAliases provides custom PHPStan types to aid static analysis.
-
ioc_service_name_string class-string<T>|non-empty-string- A
class-stringorstringname for a service.
- A
-
ioc_service_object ($serviceName is class-string<T> ? T : object)- The service
objectfor a given service name.
- The service
Implementations
Implementations MAY define additional class members not defined in these interfaces.
Notes:
- Reference implementations are available at https://github.com/ioc-interop/impl.
Q & A
How is Ioc-Interop different from PSR-11?
PSR-11 is an earlier recommendation that offers an interface to get
items from a container, and to see if that container has a particular item.
Ioc-Interop is functionally almost identical to PSR-11. However, Ioc-Interop
is intended to contain only services (object). PSR-11 is intended to contain
anything (mixed).
Ioc-Interop also offers an IocContainerFactory interface, whereas PSR-11 offers none.
Is Ioc-Interop compatible with PSR-11?
No, in the sense that the method names, signatures, and intents are different.
Yes, in the sense that both may be implemented on the same class; the method names are different, and so are non-conflicting.
Why does Ioc-Interop not afford service management?
Ioc-Interop is focused on the concerns around obtaining and consuming services. The affordances for managing and producing services are a set of separate concerns.
Earlier drafts of Ioc-Interop were much more expansive, including a resolver subsystem and a service management subsystem. These have been extracted to separate standards, each of which is dependent on Ioc-Interop:
This separation helps to maintain a boundary between the needs of service consumers (afforded by Ioc-Interop) and service producers (afforded by Service-Interop and Resolver-Interop).
Note that Ioc-Interop is independent of Service-Interop and Resolver-Interop. Ioc-Interop implementations can use them, or avoid them, as implementors see fit.
Is IocContainer for Dependency Injection or is it a Service Locator?
IocContainer acts a Service Locator only when it is used as a dependency in order to retrieve other dependencies from it.
Why does IocContainer disallow non-object values?
IocContainer is explicitly a service container, not a general config container for scalar or array values.
Limiting services to objects helps maintain consistent expectations regarding
service types and behavior. Of the researched projects, 10 return object, and
8 return mixed, so this restriction is consistent with the majority.
Ioc-Interop recognizes that implementors and consumers often want to make config values easily available, though Ioc-Interop questions what it means (or if it is possible) to get a "shared" scalar or array that works the same as a "shared" object.
With that in mind, Ioc-Interop encourages the use of one or more config services or value objects to make those values available, instead of storing config values directly inside a container.
Why does IocContainer define getService() and not just get() ?
The vast majority of researched projects, whether PSR-11 conforming or not, use
the method name get(). Contra the research, Ioc-Interop asserts that get()
is too generic, and that the method name should hint at what is being gotten;
thus, getService().
ioc-interop/interface 适用场景与选型建议
ioc-interop/interface 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.93k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ioc-interop/interface 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ioc-interop/interface 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-03