Dependency Injection (DI) is hardly anything new. I see it as a must for Test Driven Development (TDD). The thing I like most about DI in Java is Autowired in Spring or javax.annotation.Resource, they way member variables are set “magically” and the exact details about creation and implementation is located elsewhere.
For PHP there are at least the following implementations related to Dependency Injection:
- PHP-Dependency (Pd) – A PHP Dependency Injection Framework
- Pimple – A simple Dependency InjectionContainer for PHP 5.3
- Zend Framework 2 Dependency Injection
PHP-Dependency (Pd) works with @ annotation in the constructor comment, but why involve the constructor? And is a whole framework needed? PHP is not Java.
Pimple is very much a Dependency Injection Container, not so much about the injection. Simple, yes.
When it comes to Zend Framework 2 Dependency Injection I just can’t grasp how it simplifies anything. It seems to require a lot of boilerplate.
Unsatisfied with the above and a given a dose of NIH Syndrome, I decided to create my own DI system for PHP. After a bit of coding yesterday evening and night I named it Pebble and it is already on github! 🙂
My current requirements on Pebble are as follows:
- It must be possible to inject values of specific properties on class construction
- It must work nicely in Zend Framework 1.x
- It must not require any configuration files (and especially no XML)
- It must be a single .php file for the core implementation
- It must provide an open interface for extension and customization
- It must work nicely with PHPUnit (especially with the getMock method)
- It must work with autoloading of classes in PHP
- It is allowed to use PHP 5.3 features
- It is allowed to use @-based annotations in docComment, e.g. /** @something */
However, I have not yet used Pebble with PHPUnit, so I don’t know yet if that requirement holds!
Now is a good time for you to examine example1.php! The README is currently plain-text, I’ll format it later!