Saturday, 5 December 2026

Understanding EvolvePHP 2: Application Lifecycle, Dependency Injection and Service Scopes

 

EvolvePHP 2: Application Lifecycle, Dependency Injection and Service Scopes

Imagine you're building a PHP application that processes customer orders. Your order service needs a database connection, a payment service, a logger and information about the current customer.

Should every order create a new instance of these services? Should they all be shared throughout the application? And what happens when the application handles another customer without restarting?

These questions become particularly important when working with background jobs, long-running workers and modular applications.

Dependency injection helps us manage how objects receive their dependencies, but we also need to consider how long those objects should exist and what happens when they are no longer needed.

This is an area that received considerable attention in the design of EvolvePHP 2.

Dependency injection is more than a container

Consider a simple PHP service:

final class OrderService
{
    public function __construct(
        private PaymentGateway $payment,
        private OrderRepository $orders
    ) {}
}

The order service doesn't need to know how its dependencies are constructed. It receives them through its constructor and concentrates on its own responsibilities.

This is ordinary dependency injection. You don't even need a framework to do it.

However, as an application grows and introduces more services, manually constructing every dependency can become difficult to manage.

EvolvePHP addresses this through an explicit service registry and a PSR-11-compatible container within the evolvephp/core package.

Developers register service definitions and control how those services are constructed and reused.

The current approach does not rely on automatic autowiring. There is more configuration involved, but it makes the application's dependency composition visible.

Not every service should live for the same amount of time

One mistake developers can make is treating every service as a singleton simply because creating an object repeatedly seems inefficient.

Think about the difference between application configuration and information about the currently authenticated customer.

Configuration can often be shared throughout the application's lifetime. Customer information, on the other hand, belongs to a particular execution.

Sharing both indiscriminately could introduce problems, especially in a long-running process.

EvolvePHP 2 supports three service lifetimes:

  • Application: A service is constructed lazily and reused throughout the lifetime of the root container.

  • Execution: A service is constructed lazily and reused within a particular execution scope.

  • Transient: A new instance is created each time the service is resolved.

In our order-processing example, immutable configuration could be an Application service, customer-specific context could be an Execution service, and a temporary calculation object could be Transient.

The appropriate lifetime depends on the object's responsibilities and whether it holds mutable state.

EvolvePHP also prevents an Application service from resolving an Execution service through its root container. This helps avoid a situation where a long-lived object accidentally retains a dependency that belongs to a shorter-lived execution.

What happens when an EvolvePHP application starts?

In the previous article, I discussed modules and plugins and how they contribute functionality to an application.

The application kernel is responsible for coordinating their startup.

Before enabled components begin their lifecycle, Core validates the configuration and resolves declared component relationships.

The components then register their service definitions. Once registration succeeds, the service registry is frozen, preventing further changes to those definitions.

Components proceed through their boot() methods, followed by ready() once all enabled components have booted successfully.

Freezing the registry doesn't mean every service is immediately constructed. Services can still be created lazily when needed.

When the application shuts down, successfully booted components are shut down in reverse boot order.

Having a defined startup and shutdown sequence makes it easier to reason about when resources become available and who is responsible for releasing them.

Keeping one execution separate from another

Application startup is different from processing an individual request, command or background job.

Suppose a worker processes an order for Customer A and then processes another order for Customer B.

We don't want the second operation to inherit customer-specific information from the first.

EvolvePHP's ExecutionScope and ExecutionOrchestrator provide foundations for managing this separation.

An execution can have its own context and scoped services. When it finishes, its scope is closed, execution-local references are released, and explicitly registered reset participants are called.

There is also an interesting distinction in how execution failures are handled.

An operation that fails does not necessarily make the process unsafe to reuse. However, if execution cleanup fails, the orchestrator marks the process as requiring quarantine and refuses further work through that orchestrator instance.

This does not mean the framework automatically terminates or restarts the worker. That responsibility belongs to the surrounding runtime.

It also doesn't guarantee that every application is safe for concurrent execution. Developers still need to manage shared state and external resources correctly.

Why this matters

For smaller applications, some of these concerns may seem unnecessary. But they become more relevant as applications introduce additional modules, background processing and different execution environments.

I believe developers should be able to understand where their dependencies come from, how long they live, and what happens to them when an operation finishes.

These foundations are available in EvolvePHP 2 Beta, although the complete production runtime remains under development.

If you're interested in dependency injection, container design or persistent PHP workers, I'd particularly appreciate your thoughts on this approach.

You can explore the Core package and its documentation here:

https://github.com/josiahking/evolvephp/tree/2.0.0-beta.1/packages/core

The public beta is available at:

https://github.com/josiahking/evolvephp/releases/tag/2.0.0-beta.1

No comments:

Post a Comment