\Horde_Injector

Injector class for injecting dependencies of objects

This class is responsible for injecting dependencies of objects. It is inspired by the bucket_Container's concept of child scopes, but written to support many different types of bindings as well as allowing for setter injection bindings.

Summary

Methods
Properties
Constants
__construct()
createChildInjector()
__call()
addBinder()
getBinder()
setInstance()
createInstance()
getInstance()
hasInstance()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
_bind()
_addBinder()
_getBinder()
$_bindings
$_instances
$_parentInjector
$_reflection
N/A

Properties

$_bindings

$_bindings : array

Type

array

$_instances

$_instances : array

Type

array

$_reflection

$_reflection : array

Reflection cache.

Type

array

Methods

__construct()

__construct(\Horde_Injector_Scope  $injector) 

Create a new Injector object.

Every injector object has a parent scope. For the very first Horde_Injector, you should pass it a Horde_Injector_TopLevel object.

Parameters

\Horde_Injector_Scope $injector

The parent scope.

createChildInjector()

createChildInjector() : \Horde_Injector

Create a child injector that inherits this injector's scope.

All child injectors inherit the parent scope. Any objects that were created using getInstance, will be available to the child container. The child container can set bindings to override the parent, and none of those bindings will leak to the parent.

Returns

\Horde_Injector

A child injector with $this as its parent.

__call()

__call(  $name,   $args) : \Horde_Injector_Binder

Method overloader. Handles $this->bind[BinderType] type calls.

Parameters

$name
$args

Returns

\Horde_Injector_Binder

See _bind().

addBinder()

addBinder(string  $interface, \Horde_Injector_Binder  $binder) : \Horde_Injector

Add a Horde_Injector_Binder to an interface

This is the method by which we bind an interface to a concrete implentation or factory. For convenience, binders may be added by $this->bind[BinderType].

bindFactory - Creates a Horde_Injector_Binder_Factory
bindImplementation - Creates a Horde_Injector_Binder_Implementation

All subsequent arguments are passed to the constructor of the Horde_Injector_Binder object.

Parameters

string $interface

The interface to bind to.

\Horde_Injector_Binder $binder

The binder to be bound to the specified $interface.

Returns

\Horde_Injector

A reference to itself for method chaining.

getBinder()

getBinder(string  $interface) : \Horde_Injector_Binder

Get the Binder associated with the specified instance.

Binders are objects responsible for binding a particular interface with a class. If no binding is set for this object, the parent scope is consulted.

Parameters

string $interface

The interface to retrieve binding information for.

Returns

\Horde_Injector_Binder

The binding set for the specified interface.

setInstance()

setInstance(string  $interface, mixed  $instance) : \Horde_Injector

Set the object instance to be retrieved by getInstance the next time the specified interface is requested.

This method allows you to set the cached object instance so that all subsequent getInstance() calls return the object you have specified.

Parameters

string $interface

The interface to bind the instance to.

mixed $instance

The object instance to be bound to the specified instance.

Returns

\Horde_Injector

A reference to itself for method chaining.

createInstance()

createInstance(string  $interface) : mixed

Create a new instance of the specified object/interface.

This method creates a new instance of the specified object/interface. NOTE: it does not save that instance for later retrieval. If your object should be re-used elsewhere, you should be using getInstance().

Parameters

string $interface

The interface name, or object class to be created.

Returns

mixed —

A new object that implements $interface.

getInstance()

getInstance(string  $interface) : mixed

Retrieve an instance of the specified object/interface.

This method gets you an instance, and saves a reference to that instance for later requests.

Interfaces must be bound to a concrete class to be created this way. Concrete instances may be created through reflection.

It does not gaurantee that it is a new instance of the object. For a new instance see createInstance().

Parameters

string $interface

The interface name, or object class to be created.

Returns

mixed —

An object that implements $interface, but not necessarily a new one.

hasInstance()

hasInstance(string  $interface) : boolean

Has the interface for the specified object/interface been created yet?

Parameters

string $interface

The interface name or object class.

Returns

boolean —

True if the instance already has been created.

_bind()

_bind(string  $type, array  $args) : \Horde_Injector_Binder

Method that creates binders to send to addBinder(). This is called by the magic method __call() whenever a function is called that starts with bind.

Parameters

string $type

The type of Horde_InjectorBinder to be created. Matches /^Horde_InjectorBinder(\w+)$/.

array $args

The constructor arguments for the binder object.

Returns

\Horde_Injector_Binder

The binder object created. Useful for method chaining.

_addBinder()

_addBinder(  $interface, \Horde_Injector_Binder  $binder) 

Parameters

$interface
\Horde_Injector_Binder $binder

_getBinder()

_getBinder(string  $interface) : \Horde_Injector_Binder

Get the Binder associated with the specified instance.

Parameters

string $interface

The interface to retrieve binding information for.

Returns

\Horde_Injector_Binder

The binder object created. Useful for method chaining.