The SitePoint PHP blog has posted a tutorial showing you how to build something that's become an integral part of most frameworks and applications practicing modern PHP development: a dependency injection container.
A search for “dependency injection container” on packagist currently provides over 95 pages of results. It is safe to say that this particular “wheel” has been invented.However, no chef ever learned to cook using only ready meals. Likewise, no developer ever learned programming using only “ready code”. In this article, we are going to learn how to make a simple dependency injection container package.
He walks you through the entire process of creating the container and each of the pieces that make it up:
- the overall interface for the container (with has/get methods)
- having it cooperate with the "container interoperability" structure
- reference handling
- constructor injection functionality
Code is included the whole way showing you how to create the parts and fit them together to make more of a whole. He ends the post with a look at a few other dependency injection containers and a reference to the repository with the end result of the article.