Thursday, October 28, 2010

Dependency Injection in PHP vs Java

How to do Dependency Injection in PHP vs Java.

Plain PHP:

$helper_sales = new HelperSales();

Magento proprietary SPI:

// no type information!
$helper_sales = Mage::helper('sales');

Java / CDI :

@Inject
private HelperSales helperSales;

Java / get bean from Spring context :

HelperSales helperSales = appCtx.getBean(HelperSales.class);

The Java examples apply to Scala as well, of course.

Still envy PHP?

1 comment:

  1. You might want to try Ding (http://marcelog.github.com/Ding) a dependency injection and inversion of control container for PHP 5.3 that is modeled after Spring(tm) for Java, supports JSR 250 and 330 annotations, xml and yaml as bean definitions, has AOP and also a MVC implementation.

    Regards!

    ReplyDelete