PHPackages                             x-wp/di - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. x-wp/di

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

x-wp/di
=======

The dependency injection container for WordPress

v1.10.0(2mo ago)314.3k1[3 issues](https://github.com/x-wp/di/issues)[1 PRs](https://github.com/x-wp/di/pulls)9GPL-2.0-onlyPHPPHP &gt;=8.1 &lt;8.5CI passing

Since Oct 3Pushed 2mo agoCompare

[ Source](https://github.com/x-wp/di)[ Packagist](https://packagist.org/packages/x-wp/di)[ GitHub Sponsors](https://github.com/seebeen)[ RSS](/packages/x-wp-di/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (37)Versions (55)Used By (9)

XWP-DI
======

[](#xwp-di)

### Dependency Injection Container for WordPress

[](#dependency-injection-container-for-wordpress)

[![Packagist Version](https://camo.githubusercontent.com/e753d062206160a4667dd842925dc2259dbb33027f2716498f64f4b214401b1d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f782d77702f64693f6c6162656c3d52656c65617365267374796c653d666c61742d737175617265)](https://packagist.org/packages/x-wp/di)[![Packagist PHP Version](https://camo.githubusercontent.com/95a4eed39b1b05de254a4f99920a62218d61350589784383a64ddfc35e81dd79/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f782d77702f64692f7068703f6c6162656c3d504850266c6f676f3d706870266c6f676f436f6c6f723d7768697465266c6f676f53697a653d6175746f267374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/95a4eed39b1b05de254a4f99920a62218d61350589784383a64ddfc35e81dd79/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f782d77702f64692f7068703f6c6162656c3d504850266c6f676f3d706870266c6f676f436f6c6f723d7768697465266c6f676f53697a653d6175746f267374796c653d666c61742d737175617265)[![Static Badge](https://camo.githubusercontent.com/53b9befaa3c2cb4e5ea567a2d7d845f224fc6eee200769e363f31d9a2fb1e1a1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f57502d253345253344362e342d3338353865393f7374796c653d666c61742d737175617265266c6f676f3d776f72647072657373266c6f676f53697a653d6175746f)](https://camo.githubusercontent.com/53b9befaa3c2cb4e5ea567a2d7d845f224fc6eee200769e363f31d9a2fb1e1a1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f57502d253345253344362e342d3338353865393f7374796c653d666c61742d737175617265266c6f676f3d776f72647072657373266c6f676f53697a653d6175746f)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/8f1e27766f9e3696c891023231ec5f65f94c7f4fdc8642a4a52f7c3f9c88067b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f782d77702f64692f72656c656173652e796d6c3f6c6162656c3d4275696c64266576656e743d70757368267374796c653d666c61742d737175617265266c6f676f3d676974687562616374696f6e73266c6f676f436f6c6f723d7768697465266c6f676f53697a653d6175746f)](https://github.com/x-wp/di/actions/workflows/release.yml)

This library allows you to implement [dependency injection design pattern](https://en.wikipedia.org/wiki/Dependency_injection) in your WordPress plugin or theme. It provides a simple and easy-to-use interface to manage dependencies and hook callbacks.

Key Features
------------

[](#key-features)

1. Reliable - Powered by [PHP-DI](https://php-di.org/), a mature and feature-rich dependency injection container.
2. Interoperable - Provides PSR-11 compliant container interface.
3. Easy to use - Reduces the boilerplate code required to manage dependencies and hook callbacks.
4. Customizable - Allows various configuration options to customize the container behavior.
5. Flexible - Enables advanced hook callback mechanisms.
6. Fast - Dependencies are resolved only when needed, and the container can be compiled for better performance.

Installation
------------

[](#installation)

You can install this package via composer:

```
composer require x-wp/di
```

Tip

We recommend using the `automattic/jetpack-autoloader` with this package to prevent autoloading issues.

Usage
-----

[](#usage)

Below is a simple example to demonstrate how to use this library in your plugin or theme.

### Creating the Application and Container

[](#creating-the-application-and-container)

You will need a class which will be used as the entry point for your plugin/theme. This class must have a `#[Module]` attribute to define the container configuration.

```
