PHPackages                             thanks-to-it/wp-dich - 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. thanks-to-it/wp-dich

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

thanks-to-it/wp-dich
====================

121.1k1PHP

Since Feb 3Pushed 4y ago3 watchersCompare

[ Source](https://github.com/thanks-to-it/wp-dich)[ Packagist](https://packagist.org/packages/thanks-to-it/wp-dich)[ RSS](/packages/thanks-to-it-wp-dich/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

WP\_DICH
========

[](#wp_dich)

**WP\_DICH** offers a way to work with WordPress hooks smartly using a Dependency Injection Container, allowing lazy loading, making classes to be loaded only when required.

The Challenge
-------------

[](#the-challenge)

Let's suppose you simply want to call a method `method_a()` from a class `Any_Class` only when a specific WordPress hook is run. How would you do it? There are at least 3 ways that come to my mind:

1. ### 👎 Object Method Call

    [](#-1-object-method-call)

The problem here is you are initializing `Any_Class()` before it's necessary, as you only need the method on `wp_footer` hook.

```
class Any_Class{
	public function method_a(){}
}
$any_class = new Any_Class();
add_action( 'wp_footer', array( $any_class, 'method_a') );
```

2. ### 👎 Using Anonymous Functions

    [](#-1-using-anonymous-functions)

The disadvantage here is you're not able to remove the action with `remove_action()`

```
class Any_Class{
	public function method_a(){}
}
add_action( 'wp_footer', function(){
	$any_class = new Any_Class();
	$any_class->method_a();
});
```

3. ### 👎 Using Static Methods

    [](#-1-using-static-methods)

With the Static Methods approach, at least your class is loaded at the proper time, but quite often, from a design standpoint, it's better to stick to non-static methods. You can't override them, they are more difficult to test and you end up having to design other things around it as static too.

```
class Any_Class{
	public static function method_a(){}
}
add_action( 'wp_footer', array('Any_Class', 'method_a') );
```

---

### 👌 The WP\_DICH Solution

[](#ok_hand-the-wp_dich-solution)

**WP\_DICH** combines the advantage of the Object Method Call, using a non static method, with the benefits of loading the class only when it's required. Check how it's simple:

First you need to pass a Dependency Injection Container Interface to `WP_DICH()`.

```
$dic  = new \Thanks_To_IT\WP_DICH\DIC();
$dich = new \Thanks_To_IT\WP_DICH\WP_DICH( $dic );
```

> WP\_DICH already offers a small Dependency Injection Container, thanks to [Carl Alexander](https://carlalexander.ca/dependency-injection-wordpress/), but you can use any library you want, like [thephpleague/container](https://github.com/thephpleague/container) or [php-di](http://php-di.org/) for example. You just have to implement a `Psr\Container\ContainerInterface` with 2 methods, `get()` and `has()`

Then you just have to setup your container as you like:

```
$dic['any_class_alias'] = function () {
	return new Any_Class();
};
```

Now if you create your hook using the class alias you've configured on the container as the first parameter of the array, instead of calling a static method, it will load your class only when `wp_footer` hook is run and then `method_a` will be called.

```
$dich->add_action( 'wp_footer', array( 'any_class_alias', 'method_a') );
```

WP\_DICH hooks
--------------

[](#wp_dich-hooks)

You can use the WordPress hook functions you are used to, like:

- `$dich->add_action`
- `$dich->add_filter`
- `$dich->remove_action`
- `$dich->remove_filter`

Services
--------

[](#services)

As we are using a Dependency Injection Container Library we may benefit from some interesting features, like services, allowing to instantiate your classes only once. Yes, no need to think about Singletons anymore.

```
$dic['any_class_alias'] = $dic->service( function () {
	return new Any_Class();
} );
```

Now everytime you call for **'any\_class\_alias'** the same object will be returned instead of creating a new one every time.

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

[](#installation)

**Composer**Add `thanks-to-it/wp-dich` to the require-dev or require section of your project's `composer.json` configuration file, and run 'composer install':

```
{
    "require": {
        "thanks-to-it/wp-dich":"dev-master"
    }
}
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity29

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/0b4b75988e6d015150217b3b9730d2341eae639521e0d6de0578b48b94f35d11?d=identicon)[pablo.pacheco](/maintainers/pablo.pacheco)

---

Top Contributors

[![pablo-sg-pacheco](https://avatars.githubusercontent.com/u/8193404?v=4)](https://github.com/pablo-sg-pacheco "pablo-sg-pacheco (52 commits)")

---

Tags

dependency-injection-containerhookslazy-loadingwordpress

### Embed Badge

![Health badge](/badges/thanks-to-it-wp-dich/health.svg)

```
[![Health](https://phpackages.com/badges/thanks-to-it-wp-dich/health.svg)](https://phpackages.com/packages/thanks-to-it-wp-dich)
```

###  Alternatives

[makeabledk/laravel-factory-enhanced

180118.6k4](/packages/makeabledk-laravel-factory-enhanced)[lukeraymonddowning/self-healing-urls

16715.9k](/packages/lukeraymonddowning-self-healing-urls)[yii2mod/yii2-sweet-alert

Alert widget based on SweetAlert extension {@link http://tristanedwards.me/sweetalert)

43154.0k2](/packages/yii2mod-yii2-sweet-alert)[heureka/inflection

Czech inflection library

6163.9k](/packages/heureka-inflection)[ptachoire/php-dmtx

Datamatrix r/w based on libdmtx &lt;http://www.libdmtx.org/&gt;

1868.3k](/packages/ptachoire-php-dmtx)[cybercog/yii2-google-analytics

Google Analytics Widget for the Yii2 framework

1677.1k2](/packages/cybercog-yii2-google-analytics)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
