IoC Container in PHP (Part 2)

Tram Ho

In this article, we will implement the implementation of IoC Container.

1. Initalize project

Above is the directory structure for our project creation. The contents of the files, respectively, will be as follows:

composer.json

src / Illuminate / Container / Container.php

src / Illuminate / Contracts / Container / Container.php

tests / Container / ContainerTest.php

That is the content of our 4 main files in this post.

If we run the unit test at this point, the test will definitely fail (RED), and this is what we want, in the spirit of TDD .

Our work to do now is to make the test pass (GREEN).

2. Implement Container

2.1. Closure resolution

We’ll implement the simplest form of the container, as it only does resolve closures that are binding. The idea is relatively simple.

bind this with that . make that then give me this

The idea of ​​implement is we will have an array of bindings[] to store all the bindings.

The implementation is quite simple, now let’s try our unit test again

Hurray! Passed!

2.1. Singleton

… To Be Continued!

Thank you for reading the article, see you in the next post, we will proceed to implement the singleton form of binding!


Code: https://github.com/tunt-1890/phpioccontainer

Share the news now

Source : Viblo