20

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.

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54
George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • Have a look at my dependency injection project for PHP 5: https://github.com/jamolkhon/Sharbat – Jamol Oct 06 '11 at 12:26
  • why is this question closed as off-topic? This is one of the most important topic these days –  Apr 20 '14 at 19:31
  • I agree, the question was if any exist, not a recommendation (and was asked six years ago). But as there is no "correct" answer I can see why it could be seen as off-topic . – George Mauer Apr 21 '14 at 14:19

7 Answers7

8

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

crono
  • 3,603
  • 3
  • 27
  • 24
6

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.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
troelskn
  • 115,121
  • 27
  • 131
  • 155
3

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

marcelog
  • 7,062
  • 1
  • 33
  • 46
3

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

DrDol
  • 2,220
  • 2
  • 19
  • 23
2

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)

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • +1 I know some guys from Zend and I know they've been working the last few years to make the concept more natural in php and more understood in the php community. – George Mauer Nov 12 '12 at 15:15
1

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?

Beau Simensen
  • 4,558
  • 3
  • 38
  • 55
0

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

Apps 4 U
  • 239
  • 4
  • 16