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

Monday, 30 November 2026

Scaling Up vs Scaling Out in PHP: When Should You Choose Each Approach?

Scaling Up vs Scaling Out in PHP: When Should You Choose Each Approach?
Your PHP application has been running smoothly for months. Traffic is growing, more users are signing up, and everything seems to be moving in the right direction.

Then something changes.

Pages start loading slowly during busy periods. Database queries take longer. Requests occasionally time out, and CPU or memory usage keeps climbing.

At this point, someone suggests upgrading the server. Another developer recommends adding more servers behind a load balancer.

Both suggestions can make sense, but they solve different problems.

Knowing when to scale up and when to scale out can save a business from spending money on infrastructure that doesn't address the real bottleneck.

Scaling Up: Getting More From One Server

Vertical scaling, commonly called scaling up, means increasing the resources available to an existing server.

For example, your PHP application might currently run on a virtual machine with 2 CPU cores and 4 GB of RAM.

Upgrading to 8 CPU cores and 16 GB of RAM gives that server more resources to handle its workload.

For a PHP application using PHP-FPM, additional CPU and memory may allow more requests to be processed concurrently, provided the FPM configuration, database capacity, and other dependencies can support them.

This is often the simplest place to start.

You don't necessarily need to change your application architecture, introduce a load balancer, or manage several application instances.

But scaling up has limits.

A bigger server doesn't automatically fix inefficient database queries, memory leaks, slow external APIs, or poorly designed application logic.

There is also a practical ceiling to how much CPU and memory you can add. And if your application depends on a single server, increasing its capacity doesn't remove that single point of failure.

Scaling Out: Adding More Application Instances

Horizontal scaling, or scaling out, means distributing work across multiple servers or application instances.

Instead of running your PHP application on one powerful server, you might run it on three smaller servers behind a load balancer.

Incoming requests are distributed across those instances, allowing the application tier to handle more work.

This approach can also improve availability when properly designed. If one application instance fails, healthy instances may continue serving traffic.

However, scaling out introduces problems that a single-server application may never have encountered.

Imagine your application stores user sessions in files on the local server.

A customer logs in through Server A. Their next request reaches Server B, which doesn't have access to the session file.

Suddenly, the customer appears to be logged out.

The application worked perfectly on one server, but adding another instance exposed an architectural assumption.

The same problem can occur with locally stored uploads, temporary files, and application state held in memory.

For horizontal scaling to work reliably, application instances need a clear strategy for managing shared or persistent state.

That might mean using shared session storage, moving user uploads to object storage, and keeping request-specific state out of long-lived global services.

This is why architectural decisions made early in development can affect how easily an application scales later.

More Servers Won't Fix Every Bottleneck

Suppose your PHP application receives thousands of requests, and each request executes several inefficient database queries.

Adding more application servers increases the number of requests that can reach the database simultaneously.

If the database is already struggling, the additional traffic may make things worse.

The same applies to external services, poorly configured caches, and background processing systems.

Scaling requires understanding where the actual bottleneck exists.

Before increasing infrastructure, I would examine a few things:

  • How much CPU and memory are the application instances actually using?

  • Are requests waiting on PHP processing, database queries, or external services?

  • Is the application using caching effectively?

  • Are background jobs competing with web requests for resources?

  • What happens to response times and error rates as concurrent traffic increases?

Tools such as application performance monitoring, database query analysis, structured logs, and load testing can help answer these questions.

The goal is to improve the part of the system that's limiting performance, not simply add more hardware.

When Should You Scale Up or Scale Out?

For a relatively small PHP application, scaling up may be a perfectly reasonable choice.

It is simpler to operate, can be cost-effective, and may provide enough capacity for a considerable period.

Horizontal scaling becomes more attractive when one server can no longer meet capacity requirements, traffic varies significantly, or the application needs better resilience against instance failures.

Even then, multiple instances don't guarantee high availability. Load balancers, databases, networking, and other dependencies also need appropriate redundancy and recovery plans.

And the two approaches aren't mutually exclusive.

A system can run multiple application servers while also increasing the resources of its database server or adjusting the capacity of its background workers.

The right combination depends on the workload, operational requirements, and budget.

Design for Growth Without Overcomplicating Everything

One mistake developers can make is designing a small application as though it already serves millions of users.

Another is building an application that assumes it will never grow beyond a single server.

Neither approach is particularly helpful.

You don't need distributed infrastructure from the beginning, but it's worth avoiding unnecessary dependencies on local server state.

Keep application responsibilities clear. Use appropriate storage boundaries. Make background operations reliable. Monitor performance before guessing what needs improvement.

These practices make it easier to respond when traffic increases, whether that means upgrading one server or introducing several.

Scaling is not a competition between bigger machines and more machines.

It's about understanding your application's workload and choosing the simplest architecture that meets its current requirements while leaving room for future growth.


The real question isn't how many servers your application runs on. It's whether your architecture can handle the growth you expect without becoming unnecessarily difficult to operate.


Tuesday, 24 November 2026

Understanding Modules and Plugins in EvolvePHP 2: Building Applications That Are Easier to Change

Understanding Modules and Plugins in EvolvePHP 2: Building Applications That Are Easier to Change

Imagine you're developing an e-commerce application. You start with products and orders, then add payments, inventory management, notifications, and reporting.

Initially, everything works fine. But as the application grows, different features begin depending on one another. A change to the payment system unexpectedly affects order processing. Removing an integration requires changes across several parts of the codebase.

Getting Started with EvolvePHP 2 Beta: What to Explore First

 

Getting Started with EvolvePHP 2 Beta: What to Explore First

The EvolvePHP 2 Beta is now publicly available, and I'm looking forward to seeing what other PHP developers think of it.

But I also understand that trying a new framework takes time. Before installing anything, most developers want to know what the project offers, what actually works, and whether it's worth exploring.

That's why I want to walk through a few practical things you can do with the beta, even if you've never heard of EvolvePHP before.

EvolvePHP 2 Beta Is Here: A New Chapter for the Framework

EvolvePHP 2 Beta Is Here: A New Chapter for the Framework

Building a framework is one thing. Getting it to a point where other developers can install it, explore the code, question the decisions, and contribute improvements is another.

After a great deal of development, testing, architectural work, and refinement, EvolvePHP 2 Beta is now available for the PHP community to explore.

Wednesday, 4 November 2026

Concurrency and Long-Running Workers in PHP: Managing State Safely

Concurrency and Long-Running Workers in PHP: Managing State Safely

A bug that appears only after an application has been running for several hours can be surprisingly difficult to track down.

A request works perfectly, then another request behaves strangely. Memory usage keeps increasing. A background job uses data it should never have seen. Restarting the worker fixes the problem, but only temporarily.

Tuesday, 3 November 2026

Using Queues to Handle Heavy Work in PHP Applications

Using Queues to Handle Heavy Work in PHP Applications

Imagine someone placing an order on your website. The application saves the order, sends a confirmation email, generates an invoice, updates an external system, and perhaps prepares a report before showing the customer a success message.

It works, but the customer has to wait for everything to finish.

Saturday, 31 October 2026

5 Caching Strategies for Scalable PHP Applications: Faster Performance Without Serving Incorrect Data

Caching Strategies for Scalable PHP Applications: Faster Performance Without Serving Incorrect Data

When a PHP application becomes slow, developers often focus on optimizing database queries, upgrading servers, or adding more infrastructure.

But sometimes, the problem is simpler: the application keeps doing the same expensive work repeatedly.

Imagine an e-commerce website fetching product categories from MySQL every time someone opens a page.

6 Ways To Scaling PHP Databases: Why the Database Often Becomes the Real Bottleneck

Scaling PHP Databases: Why the Database Often Becomes the Real Bottleneck

When a PHP application becomes slow, the first instinct is often to increase server resources.

More CPU. More RAM. More application servers.

But what happens when the application servers are no longer the problem?

You can scale PHP horizontally and still experience poor performance because every application instance depends on the same overloaded database.

Friday, 23 October 2026

PHP2Shell: A Wake-Up Call to Protect and Fund the Language Powering the Web

 

PHP2Shell: A Wake-Up Call to Protect and Fund the Language Powering the Web

On October 21, 2026, the PHP development team released emergency security updates addressing two critical vulnerabilities collectively known as PHP2Shell.

Both vulnerabilities carry a severity score of 9.2 out of 10 and could allow attackers to execute malicious code on vulnerable servers without authentication.