Binding lifetimes
- Comparing
- v3 to v4
- Current
- Read the document
Abstract
We I will replace the shared flag on a binding with a lifetime of
three states: Process, resolved once and held until the worker restarts; Cycle, resolved once per
cycle and discarded when the cycle closes; and Transient, a new instance on every resolution.
The container will open and close cycles for whatever drives it, and dispose of cycle instances that hold resources when
a their cycle closes. Where a longer-lived object needs an instance from the current cycle, the container will inject a
provider.
Motivation
The container records whether a binding is shared, and nothing else about how long an instance lasts. A shared instance is cached for the life of the container.
The panel runs as a long-lived worker, as ADR-0009 decides. When a PHP process lasted one request, a shared instance lasted one request. In a worker, the same binding lasts until the worker restarts. The lifetime of every binding changed when the runtime was chosen, without any binding changing.
There is no way to declare one instance per request, discarded afterwards, which anything holding the identity of a request needs. The same need arises for a job taken by a queue worker, and for a tick of the scheduler.
Proposal
LifetimesThree lifetimes
Every resolution will have a lifetime, one of the three cases of
the Lifetime enum:
| Lifetime | Duration | Cached |
|---|---|---|
Process |
Resolved once, held until the worker restarts. | In the strong cache, or the weak cache if liminal. |
Cycle |
Resolved once per cycle, discarded when the cycle closes. | In the cycle cache. |
Transient |
A new instance on every resolution. | Never. |
A resolution's lifetime will be decided in this order:
- A lifetime declared on its binding. A binding holding an instance declares
Process. - Otherwise, a lifetime declared with
PerProcessorPerCycleby the requestedclass,class after alias normalisation, or by the binding's concrete class,: the two classes RFC-0006 reads class-level attributes from. Where the two classes declare different lifetimes, or one class carries both attributes, the resolution throwsLifetimeException. - Otherwise,
Processfor a class with a binding, andTransientfor a class without one. - A liminal resolution whose lifetime would be
TransientisProcess, as described under Liminality.
Declaring a lifetime
A binding will declare its lifetime with lifetime(), or with
transient() as a shorthand for Transient, which replaces notShared():
$registry->bind(ServerRepository::class)->to(SqlServerRepository::class); // Process
$registry->bind(CurrentUser::class)->using($factory)->lifetime(Lifetime::Cycle); // Cycle
$registry->bind(Report::class)->transient(); // Transient
| Builder method | Effect |
|---|---|
lifetime(Lifetime $lifetime) |
Declares the binding's lifetime. |
transient() |
Declares the binding Transient. It replaces notShared(). |
disposeUsing(Closure $callback) |
Declares how an instance of a Cycle binding is disposed, as described under Disposal. |
A binding holding an instance will always be , since its instance is the result of every resolution.
Declaring BindingBuilderProcessCycle and or on one will BindingTransientcarry the lifetimethrow LifetimeException of a binding
and its disposal callback in place of when the shared flag. The shared flag will be removed, not kept alongside binding is built from its builder,
whatever order the lifetimebuilder's methods were called in.
A binding holding an instance will always be Process, since its instance is the result of every resolution. Declaring
Cycle or Transient on one will throw LifetimeException when the binding is built from its builder, whatever order
the builder's methods were called in.
BindingBuilder and Binding will carry the lifetime of a binding, and its disposal callback, in place of the shared
flag. The shared flag will be removed rather than kept alongside the lifetime.
A class will declare its own lifetime with an attribute, which will apply whether or not it has a binding, unless its binding declares one:
#[PerCycle]
final class CurrentUser {}
#[PerProcess]
final class GameCatalogue {}
An abstract abstract and its concrete concrete class that both declare a lifetime lifetime
will have to declare the same one. A binding's declared
declared lifetime overrides the attributes of both classes, so the the
binding is where an implementation is given a lifetime
lifetime different from the one its abstract declares:
$registry->bind(PermissionChecker::class)->to(CachingPermissionChecker::class)->lifetime(Lifetime::Cycle);
PerProcess and PerCycle will be markers tested only for presence, memoised with the other class attributes, as
ADR-0014 decides. There will be no attribute for
attribute
for Transient, since a class with no binding will be is Transient already.
Liminality
Liminality will apply to the Process lifetime alone:
- A liminal
Processresolution will be held in the weak cache. - A liminal resolution that would otherwise be
Transientwill be held weakly asProcess. - A liminal resolution of a
Cyclebinding or class will stayCycle, and its liminality will be ignored.
There will be no weak cycle cache. A weakly held instance lasts as long as something else holds it, and a
cycle instance will last lasts until its cycle closes. Combining them the two would give whichever ends first,
and each bound would make the other redundant.
Cycles
| Method | Effect |
|---|---|
openCycle(): void |
Opens a cycle. Throws if one is already open. |
closeCycle(): void |
Disposes of the cycle's instances, discards them, and closes the cycle. Throws if none is open. |
inCycle(): bool |
Returns whether a cycle is open. |
The container Cycles will not know what nest. Resolving a cycleCycle lifetime representswith no cycle open will throw, rather than
return an instance with the wrong identity. Whatever drives it The check will open and close
cyclesbe made when the lifetime is decided, such as so a worker serving HTTP around each request, lazy resolution of
a queue worker around each jobCycle lifetime will throw when it is requested with no cycle open, or a scheduler around each
tickand its lazy proxy
will throw when it is first used with none open.
Cycles will not nest. Resolving a Cycle lifetime with no cycle open will throw, rather than
return an instance with the wrong identity. The check will be made when the lifetime is decided, so a lazy resolution of
a Cycle lifetime will throw when it is requested with no cycle open, and its
lazy proxy will throw when it is first used with none open.
The container will not know what a cycle represents. Whatever drives it will open and close
cycles, such as a worker serving HTTP around each request, a queue worker around each job, or the scheduler around each
tick. It will do so with three methods on the container: openCycle() opens a cycle and throws if one is already open,
closeCycle() disposes of the cycle's instances, discards them and closes the cycle, throwing if none is open, and
inCycle() reports whether a cycle is open.
Caches
The container will hold three caches of the instance cache type described in
that
RFC-0006: describes: the strong cache and the weak cache it already has, and a strong
cycle cache. A single method, cacheFor(), will select the cache from a resolution's
liminality and lifetime, and both reading and writing will call it. It will select no cache
for Transient, the cycle cache for Cycle, the weak cache for a liminal Process
resolution and the strong cache for any other Process resolution.
private InstanceCache $instances = InstanceCache::strong();
private InstanceCache $liminalInstances = InstanceCache::weak();
private InstanceCache $cycleInstances = InstanceCache::strong();
One method Closing a cycle will select replace the cachecycle cache with a new one once its instances have been
disposed of, and both reading and writing so they are discarded as one object rather than removed one by one. InstanceCache itself will call it:not change.
private function cacheFor(bool $liminal, Lifetime $lifetime): ?InstanceCache
{
return match (true) {
$lifetime === Lifetime::Transient => null,
$lifetime === Lifetime::Cycle => $this->cycleInstances,
$liminal => $this->liminalInstances,
default => $this->instances,
};
}
In the resolution steps of RFC-0006, the
lifetime will be decided alongside liminality. The cached instance step will read from the
cache selected for both, and the sharing step will write to it. A Transient resolution will read from and write to no
cache. The lifetime passed to cacheFor() will be the one decided after liminality is applied, so a liminal resolution
never reaches it as Transient.
Closing a cycle will replace the cycle cache with a new one once its instances are disposed of,
so they will be discarded as one object rather than removed one by one. InstanceCache itself will not change.
In the resolution steps of RFC-0006, the lifetime will be decided alongside
liminality. The cached instance step will read from the cache selected for both, and the sharing step will write to
it, so a Transient resolution reads from and writes to no cache. The lifetime given to cacheFor() will be the one
decided after liminality is applied, so a liminal resolution never reaches it as Transient.
Ghost objects
A dependency resolved as a ghost object will follow its
lifetime. A ghost becomes the instance itself once initialised, as
RFC-0001 describes, so it can be cached as the instance before it is
initialised. GhostResolver will decide the lifetime as for a resolution of the same class
and name or qualifier, and use the cache selected for it:
- An instance already in that cache will be injected as it is,
withand no ghost will be created. - Otherwise the ghost will be created and put in that cache at once, so a later resolution of the same class
returns thereturns the same object: for the rest of the cycle for aCyclelifetime, and until theworker restartsworker restarts forProcess. - A
Transientlifetime will create a new ghost each time, cached nowhere.
A ghost will be added to the disposal list when it is initialised, after the
dependencies its constructor resolves, rather than when it is created. A ghost that is never initialised will not be disposed
be
disposed of.
A process instance depending on a cycle instance
A Process object whose constructor takes a Cycle instance will resolve it once, in the first
cycle, and hold it for the life of the worker, so every later cycle will see the first cycle's
instance. A lazy proxy of a Cycle instance will have has the same result: it will resolve
once, in resolving once in
whichever cycle first uses it. So will , and so does a ghost object held by a Process
object, ,
which is initialised once, once in whichever cycle first uses it.
The container will not reject this. A longer-lived object that needs an instance from the current cycle cycle's instance will take a
provider instead.
Providers
A provider will implement the Provider contract, whose one method returns the provided
instance:
interface Provider
{
public function get(): object;
}
A class asks for one by marking a parameter with Provide, naming the class to provide. An audit logger bound as
Process reads the current user afresh on each call:
final class AuditLogger
{
public function __construct(
#[Provide(CurrentUser::class)] private Provider $currentUser,
) {}
public function log(string $event): void
{
$user = $this->currentUser->get();
}
}
Provide will be a resolvable attribute naming the class to provide, and
and ProviderResolver its its
resolver, registered alongside GhostResolver, as
RFC-0001 describes. The resolver will build a Provider for the class and
inject it, so the consuming class never sees the container.
Provider::get() will resolve the class each time it is called, against whichever cycle is open
then, so nothing will be is captured. PHP has no generics, so it will return object; . Provider<CurrentUser> will exist
only exist only
as a docblock type for static analysis, and the attribute will be the source of truth. A test will be able to substitute
its own Provider with no container involved.
ProviderResolver will check, when it builds the provider, that the provided class has a
binding or can be constructed, without resolving it, and will throw if not. A broken binding
will therefore fail when the consuming class is built, not on the first call to get().
Providers will be for an object needing that needs an instance with a shorter lifetime than its own.
They will not be a general way to defer resolution.
Disposal
A Cycle instance holding a resource, such as an open transaction, a buffered log writer or an unsaved session, will be
disposed of when its cycle closes. Disposal will be declared in
either of two ways. A class that knows what it holds, with or without a binding, will
implement Disposable:
interface Disposable
{
public function dispose(): void;
}
A class that cannot or should not implement a container contract, such as one from a third-party library, will have
a callback declared on its binding with disposeUsing(), which receives the instance:
$registry->bind(Session::class)
->lifetime(Lifetime::Cycle)
->disposeUsing(fn (Session $session) => $session->save());
Disposable, implemented by the class, will suit a class that knows what it holds, with or without a binding.disposeUsing(), declared on the binding, will suit a class that cannot or should not implement a container contract, such as one from a third-party library. The callback will receive the instance. Where a binding declares a callback, the callback will be used instead ofdispose().
disposeUsing() will only be valid on a binding that itself declares the Cycle lifetime itself.
Declaring it on a binding that does not will throw LifetimeException when the binding is built from its builder, even
where a PerCycle attribute would make its resolutions Cycle. A class that disposes of itself, whatever declares its
lifetime, will implement Disposable.
Only Cycle instances will be disposed of. Process and Transient instances will not be, even when they are
Disposable or were created during a cycle.
Where a binding declares a callback, the callback will be used instead of dispose().
The container will keep a disposal list for the open cycle, separate from the cycle
cache. A Cycle instance that is Disposable, or whose binding declares a callback, will be appended to it once
constructed, and every other instance will be left off it. A ghost object is constructed
when it is initialised. A dependency resolved eagerly is constructed before whatever depends on it, so it will appear
earlier in the list. A dependency constructed later, through a lazy proxy or a ghost
object, will appear after whatever holds it.
When Closing a cycle closeswill run these steps in order:
- The cycle counts as closing, so resolving a
Cyclelifetime throws. - Every instance on the disposal list is disposed of, from the last appended to the first
, so an. An instance isdisposedtherefore disposed of before the dependencies it resolved eagerly, and after any constructed later through a lazy proxy oraa ghost. - Every disposal runs, even when one throws.
- The cycle cache is replaced, the disposal list is dropped, and the cycle closes.
- If any disposal threw, a
DisposalExceptioncarrying every failure is thrown.
Errors
| Exception | Extends | Thrown when |
|---|---|---|
LifetimeException |
LogicException |
A cycle is opened while one is open, closed while none is open, or a Cycle lifetime is resolved with no cycle open or while one is closing. A resolution's two classes declare different lifetimes, or one class carries both PerProcess and PerCycle. A binding holding an instance declares Cycle or Transient, or disposeUsing() is declared on a binding that does not declare Cycle. |
DisposalException |
RuntimeException |
One or more disposals failed when a cycle closed. It carries every failure. |
DependencyResolutionException |
RuntimeException |
The class a Provider provides has no binding and cannot be constructed. |
Every exception LifetimeException, extending LogicException, will implement be thrown for a misuse of lifetimes or
cycles: a cycle opened while one is open or closed while none is open; a
lifetime resolved with no cycle open or while one is closing; a
resolution whose two classes declare different lifetimes, or one class carrying both
ContainerExceptionCyclePerProcess and PerCycle; a binding holding an instance that declares Cycle or
Transient; and disposeUsing() declared on a binding that does not declare Cycle.
Every exception here will implement ContainerException.
DisposalException, extending RuntimeException, will be thrown when one or more disposals fail as a cycle closes,
and will carry every failure.
DependencyResolutionException, extending RuntimeException, will be thrown when the class a Provider provides has
no binding and cannot be constructed.
Out of scope
- Opening and closing cycles. The HTTP worker, the queue worker and the scheduler do that, and each belongs to its own design.
- Disposing of
Processinstances when the worker shuts down, which belongs to bootstrapping. - Validating the dependency graph. A
Processobject taking aCycleinstance is not rejected.
Alternatives considered
The decisions this This design rests on are two decisions recorded separately::
ADR-0009, which runs the panel as a
long-lived worker, and ADR-0014, which
memoises class-level attributes as presence flags.
Naming Keeping the middle lifetime Requestshared flag alongside the lifetime. We Both would be recorded on a binding. I
rejected this because HTTP opens a cycle for each
request, but a queue worker opens one for each job and two ways of saying the scheduler for each tick. Request same thing would put HTTP
vocabulary into the container, and read wrongly for the other twodisagree.
Keeping the shared flag alongside the lifetimeResolving a Cycle lifetime outside a cycle as Transient. We I rejected this because two ways of saying it would produce an instance
with the same thing would
disagreewrong identity, and no error.
Resolving a Cycle lifetime outside a cycle as TransientNesting cycles. We A cycle could be opened inside another. I rejected this because it a second cycle opened inside the
first would produce an instance
with the wrong identityneed a stack of cycle caches, and no errornothing needs one.
Nesting cycles.Partitioning InstanceCache We by lifetime, in place of a third cache. I rejected this because a second cycle opened inside the first would need a stack of cycle
cachesthird cache keeps
InstanceCache unchanged, and nothing needs oneclosing a cycle then discards one object instead of walking entries.
Partitioning InstanceCache by lifetimeA forwarding proxy, built on the ghost object machinery, in place of a provider. We I rejected this because a third cache keeps InstanceCache unchanged,
and closing it would
resolve a cycle discards one object instead of walking entriesdifferent instance between two calls on the same object, with nothing visible where it is used.
A forwarding proxy, built on the ghost object machinery, in place of Sharing a provider.class with no binding, as RFC-0001 We and
RFC-0006 specify. Every class the container constructs without a
binding would be Process. I rejected this because it would
resolve in a different instance between two calls on worker, a class that keeps state and
that nobody bound, such as one accumulating invoice lines, would carry that state into every later request and grow
until the same objectworker restarts. It would be easy to do, with nothing visible where it is usedand not every class can be expected to be bound.
Sharing a class with no bindingLetting an implementation shorten its abstract's lifetime., as RFC-0001 and
RFC-0006A concrete specify. Every class the container constructs without a
bindingcould declare
a shorter lifetime would be Processthan its abstract, as a return type may narrow,
but not a longer one. We I rejected this because in a workerlifetime promises both sharing, which a class that keeps state
and that nobody boundlonger one gives more of, such as and
isolation, which a shorter one accumulating invoice linesgives more of, would carry that state into so neither direction keeps every later request and grow
until the worker restartspromise. It would be easy to doA Process abstract may be
injected directly into a Process object, and not every class can be expected to a Cycle implementation of it would be boundcaptured there from the first
cycle, with nothing visible where it is used.
Letting an implementation shorten its abstractThe concrete class's lifetimelifetime winning. A The concrete class could declare a shorter
lifetime than its abstract, as a return type may narrowclass's attribute would apply, but not a longer oneand the abstract's only where
there is no concrete class. We rejected
I rejected this because a lifetime promises both sharing, which a longer one gives more of, and isolation, which a shorter one
gives more of, so neither direction keeps every promise. A Process whatever depends on the abstract may be injected directly into a Process
object, and a Cycle implementation of it would be captured there from could not rely on the first cycle, with
nothing visible where it is usedlifetime it
declares.
The concrete class's lifetime winningLeaving ghost objects outside lifetimes. The concreteA ghost object class's attribute would applystay its own instance,
created for each dependency and never cached, and
the abstract's only where there is no concrete classas in RFC-0001. We I rejected this this
because whatever depends on the abstract could
not rely on the a ghost of a Cycle or Process lifetime it declareswould be a second instance beside the cached one, and a second
Cycle instance would never be disposed of, so writes to it, such as to a session, would be lost.
Leaving ghost objects outside lifetimesIgnoring the lifetime of a binding holding an instance. A ghost object would stay its own instance,
created for each dependency and never cached, as in RFC-0001. We rejected
this because a ghost of a Cycle or lifetime declared on one would ProcessTransientbe a second instance beside the cached one, be
accepted and a second
Cycle instance would never have no effect. I rejected this because neither can be disposed ofhonoured when every resolution returns the same
instance, so writes to it, such as to declaring one is a session, would be lostmistake to report.
Ignoring the lifetime of a binding holding an instance. A Accepting CycledisposeUsing()or wherever the resolved lifetime is . TransientCyclelifetime declared on one The check would be
accepted and have no effectbe made at resolution,
after the class attributes are read. We I rejected this because a disposal callback on a binding that does not declare
Cycle or Transient lifetime
cannot would be honoured attached for nothing, and the mistake would surface only when every resolution returns the same instance, so declaring one binding is a mistake to reportfirst resolved. A class
that disposes of itself implements Disposable.
Accepting Making disposeUsing() wherever declare the resolved lifetime is Cycle lifetime. The check would be made at resolution, after
the class attributes are read. We rejected this because Declaring a disposal callback on a binding that does not declare would make it
Cycle
would be attached for nothing, and the mistake would surface only when the binding is first resolved. A class that
disposes I rejected this because a lifetime set as a side effect of itself implements Disposableanother method is easy to miss when reading the
binding.
Making disposeUsing() declare the Cycle lifetime. Declaring a disposal callback on a binding would make it
Cycle. We rejected this because a lifetime set as a side effect of
another method is easy to miss when reading the binding.
No other alternatives were weighed.
Naming the middle lifetime Request. I rejected this because HTTP opens a cycle for each
request, but a queue worker opens one for each job and the scheduler one for each tick. Request would put HTTP
vocabulary into the container, and read wrongly for the other two.
Backwards compatibility
notShared()will be replaced bytransient(), and the shared flag on a binding, and thesharedargument ofBinding's constructor, by its lifetime.- A class with no binding will be
Transient, where RFC-0001 and RFC-0006 share it. - A binding holding an instance and declared
transient()will throw. TodaynotShared()on one has no effect, and every resolution returns its instance. - A ghost object of a
Processlifetime will be the shared instance, or be cachedasas it.. Today every ghost is a separate instance, never cached.
Open questions
Changelog
- : Reworded in the readable house style, and the project's own vocabulary linked to the glossary. The design is unchanged.
- : Revised after review, against RFC-0006 as accepted. A lifetime attribute
is read from the requested class and the binding's concrete class, and a disagreement between them, or one class
carrying both attributes, throws. A binding holding an instance is always
Process.disposeUsing()requires the binding to declareCycleitself. OnlyCycleinstances are disposed of, in reverse order from a disposal list kept beside the cycle cache, so a dependency constructed later through a lazy proxy or a ghost is disposed of before whatever holds it. Ghost objects follow their lifetime. A liminal resolution that would beTransientisProcessbefore the cache is chosen, and a lazyCycleresolution with no cycle open throws when requested. RFC-0006 moves fromdependstoupdates. The alternatives for each, and the reason a class with no binding isTransient, are recorded.
Sources
- Issue #63, Engine - Container - Binding lifetimes, , edited on : the motivation, the three
lifetimes and why the middle one is not named
Request, the defaults,lifetime(),transient()and the removal of the shared flag,PerCycle, the third cache and its single selector, liminality with cycles not built, opening and closing cycles, throwing outside a cycle and for nested cycles, a process instance depending on a cycle instance, providers, disposal in reverse order with its contract left open, and the boundaries. Its edit of addedProvide,ProviderResolver, the check when the provider is built, and providers being for a lifetime mismatch only. A comment of the same day records why: the first consumer, in sessions, showed that a parameter typedProvidercarries nothing saying what to provide. - A class with no binding being
Transient, with no attribute for it; thePerProcessattribute; the order a lifetime is decided in; liminality applying toProcessalone, with a liminal resolution of aCyclelifetime stayingCycle; the provider check meaning a binding or a constructible class; lazy proxies and ghosts of cycle instances resolving once; disposal through bothDisposableanddisposeUsing(), with the binding's callback used instead ofdispose()and valid only onCyclebindings; the rules for a closing cycle; the cycle methods; and the exceptions: first written down on . - The disposal list and its order; reading a lifetime attribute from both classes and
throwingthrowing when they disagree;aa binding holding an instance beingProcess;disposeUsing()requiring a declaredCycle;; disposal ofCycleinstances only; ghost objects following their lifetime; the reason a class with no bindingisisTransient; andthethe alternatives rejected for each: first written down on .
Source rfc/0007-binding-lifetimes.md · commit 9444a99 · published