Several months ago I started using Spring Framework . That is how I faced the dependency injection . As my programming skills mostly originated from web development with PHP, I found out that I've missed a great technique, which is as usually common in Java land, but rare in PHP. I ended up with a research of dependency injection libraries for PHP. There was a port of Pico container by Pawel Kozlowski, who gave a talk on it recently, and that was almost it (some bright minds from #php.thinktank had their own solutions for that).
However, all these solutions made you to define your dependencies within the code of your application, and were quite minimalistic. That made my mind - I started working on Garden - the dependency injection container for PHP5, which would use XML definitions with syntax as similar as possible to the Spring. Today Garden is packed up and ready for use. Here is a sample application context that injects a collar on a dog:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//GARDEN//DTD BEAN//EN" "garden-beans.dtd">
<beans default-lazy-init="true">
<bean id="dog" class="Dog" file="ext/Dog.php">
<property name="collar">
<ref local="dogCollar"/>
</property>
</bean>
<bean id="dogCollar" class="Collar" file="ext/Dog/Collar.php">
<property name="spiked" value="true"/>
</bean>
</beans>
The contents of Dog and Collar classes:
class Dog
{
private $collar;
public function setCollar($collar)
{
$this->collar = $collar;
}
public function getCollar()
{
return $this->collar;
}
}
class Collar
{
private $spiked;
public function setSpiked($spiked)
{
$this->spiked = $spiked;
}
public function getSpiked()
{
return $this->spiked;
}
}
And finally, a piece of code to access the beans:
require_once 'Garden.php';
Garden::initApplicationContext('example.xml');
$ctx = Garden::getApplicationContext();
$dog = $ctx->getBean('dog');
$collar1 = $dog->getCollar();
$collar2 = $ctx->getBean('dogCollar');
var_dump($dog, $collar1, $collar1 === $collar2);
The output of dump:
object(Dog)#1 (1) {
["collar:private"]=>
object(Collar)#2 (1) {
["spiked:private"]=>
bool(true)
}
}
object(Collar)#2 (1) {
["spiked:private"]=>
bool(true)
}
bool(true)
This example reveals only a basic setter injection, however the possibilities allow to use constructor argument injection, nested bean definitions, combined XML definition resources, aliasing, and literally almost all functionality found here . I hope this will be helpful for those of you who work on large scale, and trust that PHP applications can be scalable, easily maintainable and designed well. Make your applications blossom!
Subscribe to:
Post Comments (Atom)
About Me
- Octopus
- Ordinary People that spend much time in the box
No comments:
Post a Comment