Question

I am trying to code TDD style in PHP and one of my biggest stumbling blocks (other than lack of a decent IDE) is that I have to make my own hacked together IoC container just to inject all my mock objects properly.

Has anyone used an Ioc container in PHP? All I've been able to find is PHP IOC on the ever-annoying phpclasses.org and it seems to have almost no documentation and not much of a following.

Was it helpful?

Solution

I played with some DI Frameworks for PHP, but I haven't used one in production. Have some links:

  1. http://www.stubbles.net/ which I think is the oldest I tried
  2. http://php.xjconf.net/
  3. FLOW3 - I belive this one will become a very nice framework (its beta right now)

You mentioned you would use it for TDD - so maybe have a look at Dependency Injection for Unit Tests in PHP

OTHER TIPS

Phemto is being developed again, and looks quite promising IMHO.

A few other similar projects, that you might want to look at:

bucket (Disclaimer: I'm the principal author of this one)

The php-port of picocontainer, has been around for a long time. I don't think it's being actively deveoped any more, but on the other hand, I believe that it's quite stable. It's been a long time since I looked at it though.

A rather new project, I recently stumbled upon is Crafty. Not sure how many people uses it though.

I'm also watching sphicy, which looks interesting.

You might also want to try Ding (http://marcelog.github.com/Ding) which is modeled after Spring(tm) for Java. It is a complete inversion of control and dependency injection container, which also supports AOP

What about the Symfony Dependency Injection or the PHP 5.3+ equivalent component from the symfony 2.0 project.

PHP-DI is another dependency injection container.

It features dependency injection through annotations and minimal configuration, here is an example:

class Foo {
    /**
     * @Inject
     * @var Bar
     */
    private $bar;
}

It's very easy to use, and it integrates with Zend Framework for example.

(yes I do work on this framework)

I have been working on a PHP IoC/DI Container named Substrate for the last six months. It is still very much a work in progress but it has been deployed in production for a month and a half and has been working pretty well so far.

Substrate is inspired by Spring Framework, but written with the strengths and limitations of PHP in mind. The documentation is pretty minimal at this point, but there is some sample code, including a unit testing example. Is this something that you think you might be able to use for TDD?

Laravel 4 has the best IoC and DI framework. if you dont mind using a framework the Laravel 4 is the way to go

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top