Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guice like dependency injection frameworks in PHP [duplicate]

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.

like image 571
newbie Avatar asked Feb 19 '12 10:02

newbie


People also ask

Why dependency injection is not good?

Disadvantages of Dependency Injection:Dependency injection creates clients that demand configuration details to be supplied by construction code. This can be difficult when obvious defaults are available. Dependency injection can make code difficult to trace (read) because it separates behaviour from construction.

What is PHP dependency injection?

Object Oriented ProgrammingPHPProgramming. Dependency injection is a procedure where one object supplies the dependencies of another object. Dependency Injection is a software design approach that allows avoiding hard-coding dependencies and makes it possible to change the dependencies both at runtime and compile time.

What is dependency injection in Symfony?

The Symfony DependencyInjection component provides a standard way to instantiate objects and handle dependency management in your PHP applications. The heart of the DependencyInjection component is a container which holds all the available services in the application.


1 Answers

(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.

like image 55
Jamol Avatar answered Oct 12 '22 12:10

Jamol