17

Are there any Guice like or similar dependency injection frameworks in PHP? If not are there any good dependency injection frameworks in PHP?

I have to implement same code in PHP what I have implemented in Java and I use Guice in my Java project. So it would be easier to implement PHP version, if it used similar framework.

newbie
  • 24,286
  • 80
  • 201
  • 301
  • This question answered on http://stackoverflow.com/questions/52072/inversion-of-control-container-for-php – vahid kh Apr 10 '12 at 09:49

7 Answers7

9

(Posting my comment as a response)

Have a look at my clone of Guice named Sharbat (means juice). The API is quite the same as Guice's.

It has pretty much everything you need:

  • Constructor injection (does not require @Inject annotation)
  • Field injection (regardless of visibility, using @Inject(FooBar))
  • Method injection (regardless of visibility, requires @Inject annotation)
  • Provider injection (for fields via @InjectProvider(T=FooBar), for methods via @Provider(T=FooBar, param=fooBarProvider)
  • Scopes (possible to implement a custom one)
  • AOP (method interceptors)
  • Circular dependencies

See the readme file for example usage.

Jamol
  • 2,281
  • 2
  • 28
  • 28
  • From Sharbat's README.md: "TODO: Documentation and Unit-testing" - kind of important things that they've left out... not to mention last update was around 04/2012... – rodrigo-silveira Jan 05 '14 at 07:40
5

I like the lightweight Aura.Di. The readme in the linked github project page gives you enough information to get started in a couple of minutes. It is dependant on 5.3 though.

I don't think Guice's use of annotations can be implemented in PHP in runtime without a very heavy performance impact, if that's your primary request.

chelmertz
  • 20,399
  • 5
  • 40
  • 46
  • Annotations are supported through docblocks - just like in java (xdoclets) long time ago. Doctrine does have support for them (http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/basic-mapping.html) – Kamil Tomšík Feb 19 '12 at 11:11
  • I couldn't think of a project using annotations in runtime, just thought of docblox which doesn't need to be extremely fast. Thanks for your example but I (naively and not with data to back it up) still believe it can be a performance hit for non compiled usage. – chelmertz Feb 19 '12 at 22:01
  • Yep, that's why you should cache these data in production. – Kamil Tomšík Feb 20 '12 at 09:24
3

I would like to recommend you pimple. It's very easy to use.
There is interesting presentation about this dependency injection container (author of this presentation is creator of pimple).

Jarosław Gomułka
  • 4,935
  • 18
  • 24
  • +1 for the slides from fabien – Carrie Kendall May 30 '13 at 14:29
  • I started using Pimple as it seemed like a nice way to get started with DiC in PHP... after about a week with it, I've come to the conclusion that Pimple is far more a Service Locator than an actual Dependency Injection container, since every service you register with it must be wired by hand... – rodrigo-silveira Jan 05 '14 at 07:52
3

There is the Symfony's Dependency Injection component.

Daniel Ribeiro
  • 10,156
  • 12
  • 47
  • 79
3

Here is another dependency framework to add to the list: PHP-DI

It is a container meant to be very practical to use, following convention over configuration (so you end up writing much less configuration than with other containers).

It features dependency injection through annotations (which is optional) and minimal configuration:

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

    public function hello() {
        return $this->bar->sayHello();
    }
}

Be aware that annotations are optional though, if you don't like it you don't have to use it.

It's very easy to use, and it integrates with Zend Framework and Symfony very well.

(disclaimer: I do work on this framework)

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • Your PHP-DI 4 is absolutely amazing! Could you please contribute: http://stackoverflow.com/questions/24076299/circular-dependency-injecting-objects-that-are-directly-depended-on-each-other – Ilia Ross Jun 06 '14 at 09:00
2

Check out

rg\injektor is a sophisticated dependency injection container for PHP that was inspired by Guice. Unlike other reflection based containers rg\injektor includes a factory class generator that you can use to prevent the use of reflection on production.

It features Constructor Injection, Property Injection, Provider Classes, Object Management, Named Injection and a couple more things. It is unit-tested and available via Composer.

Gordon
  • 312,688
  • 75
  • 539
  • 559
2

Let me introduce my implementation of the clone of Guice.

Ray.Di https://github.com/ray-di/Ray.Di

koriym
  • 21
  • 4