Hi,
I've seen some real potential for using Dependency Injection, but all of the code I work with is PHP4 only. So, I thought I'd see if I could make something work in PHP4.
The PHP5 features used by dependency injectors seem to be:
- interfaces
- type hints in constructor signature
I replaced both of these with static methods on the class, e.g.
Code:
class Adder implements Number {
function __construct(One one, Two two) {
}
}
becomes
Code:
class Adder {
function implements() {
return array('Number');
}
function Adder($one, $two) {
}
function constructorSignature() {
return array('One', 'Two');
}
}
The DI container will call these methods when the class is registered to find out the class meta-information. Using this approach, I've ported Marcus's phemto code to php4. The tests run pretty much unmodified. Would be interested to hear what other people think of this approach.
Dave
Bookmarks