{"id":36983910,"url":"https://github.com/thanks-to-it/wp-dich","last_synced_at":"2026-01-13T22:59:37.037Z","repository":{"id":44623589,"uuid":"238093751","full_name":"thanks-to-it/wp-dich","owner":"thanks-to-it","description":"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","archived":false,"fork":false,"pushed_at":"2022-02-03T22:46:28.000Z","size":89,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-21T08:47:18.036Z","etag":null,"topics":["dependency-injection-container","hooks","lazy-loading","wordpress"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thanks-to-it.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-04T00:54:01.000Z","updated_at":"2023-11-17T02:53:12.000Z","dependencies_parsed_at":"2022-08-24T14:23:45.153Z","dependency_job_id":null,"html_url":"https://github.com/thanks-to-it/wp-dich","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/thanks-to-it/wp-dich","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanks-to-it%2Fwp-dich","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanks-to-it%2Fwp-dich/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanks-to-it%2Fwp-dich/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanks-to-it%2Fwp-dich/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thanks-to-it","download_url":"https://codeload.github.com/thanks-to-it/wp-dich/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thanks-to-it%2Fwp-dich/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28400881,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T14:36:09.778Z","status":"ssl_error","status_checked_at":"2026-01-13T14:35:19.697Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection-container","hooks","lazy-loading","wordpress"],"created_at":"2026-01-13T22:59:36.329Z","updated_at":"2026-01-13T22:59:37.031Z","avatar_url":"https://github.com/thanks-to-it.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WP_DICH\n**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.\n\n## The Challenge\nLet'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: \n\n1. ### :-1: Object Method Call\nThe problem here is you are initializing `Any_Class()` before it's necessary, as you only need the method on `wp_footer` hook.  \n```php\nclass Any_Class{\n\tpublic function method_a(){}\n}\n$any_class = new Any_Class();\nadd_action( 'wp_footer', array( $any_class, 'method_a') );\n```\n\n2. ### :-1: Using Anonymous Functions \nThe disadvantage here is you're not able to remove the action with `remove_action()` \n```php\nclass Any_Class{\n\tpublic function method_a(){}\n}\nadd_action( 'wp_footer', function(){\n\t$any_class = new Any_Class();\n\t$any_class-\u003emethod_a();\n});\n```\n\n3. ### :-1: Using Static Methods\nWith 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. \n```php\nclass Any_Class{\n\tpublic static function method_a(){}\n}\nadd_action( 'wp_footer', array('Any_Class', 'method_a') );\n```\n\n\n---\n\n\n### :ok_hand: The WP_DICH Solution\n**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:\n\nFirst you need to pass a Dependency Injection Container Interface to `WP_DICH()`.  \n```php\n$dic  = new \\Thanks_To_IT\\WP_DICH\\DIC();\n$dich = new \\Thanks_To_IT\\WP_DICH\\WP_DICH( $dic );\n```\n\u003e 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()`\n\nThen you just have to setup your container as you like:\n```php\n$dic['any_class_alias'] = function () {\n\treturn new Any_Class();\n};\n```\n\nNow 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.\n```php\n$dich-\u003eadd_action( 'wp_footer', array( 'any_class_alias', 'method_a') );\n```\n\n## WP_DICH hooks\nYou can use the WordPress hook functions you are used to, like:\n\n- `$dich-\u003eadd_action`\n- `$dich-\u003eadd_filter`\n- `$dich-\u003eremove_action`\n- `$dich-\u003eremove_filter`\n\n## Services\nAs 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.\n\n```php\n$dic['any_class_alias'] = $dic-\u003eservice( function () {\n\treturn new Any_Class();\n} );\n```\nNow everytime you call for **'any_class_alias'** the same object will be returned instead of creating a new one every time.\n\n\n## Installation\n**Composer**\nAdd `thanks-to-it/wp-dich` to the require-dev or require section of your project's `composer.json` configuration file, and run 'composer install':\n```json\n{\n    \"require\": {\n        \"thanks-to-it/wp-dich\":\"dev-master\"\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthanks-to-it%2Fwp-dich","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthanks-to-it%2Fwp-dich","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthanks-to-it%2Fwp-dich/lists"}