Let's Talk - Building a Modular PHP Framework from Scratch

Author(s): Louis Ouellet


Have you ever worked with popular PHP frameworks like CakePHP or Symfony and thought, “I wonder how these were built?” PHP frameworks can be tremendous time-savers, but they are truly powerful only when you know them inside and out. Developing your own mini-framework can be a great learning exercise, giving you deeper insight into best practices, modularity, and maintainability.

In this article, I share how I’ve started building my own PHP framework from the ground up. This includes constructing a Bootstrap class, handling configuration, setting up modules, creating a logging system, and finally wrapping requests into a tidy Request class. By walking through each piece, you’ll see the value of a well-structured, modular approach that can be extended with custom modules as needed.

Why a Modular Approach?

Modularity is key for creating lightweight, flexible applications. A large framework can introduce overhead if you only need one or two components out of many. By splitting functionality into separate modules that can be selectively loaded, you ensure performance and maintainability.

Defining Our Core Module (“core”)

I’ve decided to name the main module simply core. This module includes any base classes that other modules may need, plus the core logic for initializing the application itself. An important detail: we want each module to initialize only once.

Bootstrap Class

The Bootstrap class manages which modules are loaded, depending on the application’s scope (e.g., Router, CLI, API). Below is the work-in-progress. Notice that we use a default configuration array and allow for custom overrides via our configuration system (shown later):

Source: Bootstrap.php

We then test the result with a short script:

Source: test.php

Fallback Module Class

One downside to having optional modules is that you need to check whether each one has been loaded (i.e., not null). Instead, we can create a default Module class that notifies the developer if a method is called on a non-installed module:

Source: Module.php

This approach gracefully halts and logs an error if the developer tries to use an uninstalled module.

Configuration Class (Replacing “Configurator”)

Next, let’s look at our updated Config class (formerly Configurator). This class manages our JSON-based configuration files:

Source: Config.php

Logger Class (coreLogger)

Below is a simplified Logger class that uses our new Config class to manage settings (like log level, rotation, etc.). It demonstrates how to chain methods for more streamlined logging:

Source: Log.php

Request Class

Finally, we have a Request class that wraps the usual superglobals ($_GET, $_POST, etc.) into a more controlled interface. We even unset the original variables to ensure developers call the class methods instead. This fosters a centralized place for data sanitization or transformation.

Source: Request.php

Conclusion

Building a custom PHP framework is a fantastic learning opportunity. By organizing your code into classes and modules, you gain a deeper understanding of how each part of a framework works—configuration, routing, logging, and request handling. This modular approach also helps keep your application lightweight and maintainable, as you can load only the modules you need.

Although this is still a work in progress, the outline above should help you get started. From here, you can add new modules (for example, a router, database handler, or templating engine) and continue refining the code. Ultimately, whether you stick to popular frameworks or roll your own, knowing how these building blocks fit together can only sharpen your skills.

I hope this provided some insights! Stay tuned for more updates as I continue developing this mini-framework. In the meantime, feel free to share your ideas or ask any questions in the comments.

Tags

Discussion

Enter your comment. Wiki syntax is allowed:
U P E I W