Skip to content

Libraries and Framework

samuelgfeller edited this page Apr 15, 2024 · 10 revisions

Introduction

Using libraries in a project significantly enhances efficiency, usability, and scalability.

Libraries contain pre-written, reusable code components that address common programming tasks or challenges. Time and effort can be saved by avoiding the need to recreate the similar functionality from scratch.

Instead of writing complex algorithms or functionalities independently, libraries can be used that have been refined and validated by a broad community.

Also, by using well-maintained and widely adopted libraries, the collective efforts of the community to identify and address potential security issues significantly reduce the risk of vulnerabilities.

In summary, using libraries promotes efficiency, consistency, and collaboration, allowing us to build robust and feature-rich applications by standing on the shoulders of the broader development community.

Too much of a good thing is a bad thing

The choice of which libraries to use has to be made with great care. Having too many dependencies can have a very negative impact on the project and be a burden in the long run.

Each library that is used has to be kept up to date and maintained. This can introduce new bugs and lead to a lot of work and time spent on refactoring the project to suit the new versions of the libraries.

There is a heavy dependency on them. They bring a sort of "rigidity" that forces the project to do it "the library way" which limits flexibility and may pose challenges when a library becomes obsolete or is no longer maintained.

Big libraries often use so much abstraction that it obscures the underlying details of how processes are executed. Debugging becomes more challenging when trying to trace and understand the interactions between abstracted components. This can make it harder to fix bugs and understand error messages.

Libraries are essential, but only the strictly necessary ones should be used in an important project meant to be maintained for a long time.

By using fewer libraries, the codebase is more lightweight and needs less maintenance. It is easier to understand and debug, and it is more flexible.

Choosing the right libraries

The framework

The framework is the foundation of the project and probably the most important dependency.

Full-stack frameworks

Full-stack frameworks often come with a wide range of built-in features and components, predefined conventions, and structures. This can lead to a steep learning curve, and it may take time to be fully comfortable programming in the framework's way.
With all the features they bring, they contain lots of code that inevitably introduces some performance overhead, but this trade-of is acceptable if the benefit of having so many built-in features is considered larger.

Full-stack frameworks are great for people or companies that don't want to invest too much time in creating a performant architecture and carefully choosing every library.

They provide a standardized way with lots of documentation, which may be easier for big teams working on the same project and new arrivals that already know the framework.

Micro frameworks

These frameworks are designed to be lightweight and minimalistic, focusing on essential parts.

They offer greater flexibility, allowing developers to choose and integrate components tailored to the project and based on their preferences.

The learning curve is often smaller, making them accessible for developers who know the language and want to quickly get started on a project.

Due to their lightweight nature, micro frameworks often result in faster performance.

While micro frameworks may have smaller communities compared to their full-stack counterparts, they often foster innovation.

SlimPHP

I created theslim-example-project with the goal of making the most simple and lightweight template that is scalable for bigger applications and maintainable in the long run.

Performance, simplicity, stability and maintainability are the guidelines of the slim-example-project, and a micro framework suits best these requirements.

The most popular PHP micro framework which I am a big fan of for its modular approach, clean codebase and excellent documentation is SlimPHP.

So the first dependency is slim/slim

Libraries

These are the libraries that I feel bring significant value to the slim-example-project:

Logger monolog/monolog

The go-to logging library for PHP is Monolog. It would make little sense to develop a custom logging library when there is a well-maintained and widely adopted one that is lightweight and easy to use.
Documentation: Logging.

Dependency Injection Container php-di/php-di

This DI-Container is a comprehensive, yet lightweight library that manages the instantiation and provision of services in the application. It is a powerful and important tool.
Documentation: Dependency Injection.

CakePHP QueryBuilder cakephp/database

This library provides a simple and flexible way to construct and execute database queries. This allows for safer, more readable, and maintainable code. It abstracts the underlying SQL and enables writing database queries using an intuitive and object-oriented syntax that produces secure queries.
Documentation: Database access with the CakePHP Query Builder.

Basepath detector selective/basepath

This is a tiny library that takes care of detecting the basepath of the application. This allows the application to be run in a subdirectory of the server without having to change the code, which is practical for development and deployment. The upside of using this library besides not having to implement it on my own is that if Apache or PHP change the way the basepath is detected, I don't have to worry about it. The library will be updated, and I can just update it via composer.

PHP-View template renderer slim/php-view

The slim/php-view library is a simple and straightforward PHP templating solution. The killer feature is that it supports native PHP templates and blends in seamlessly with the rest of the project. It is lightweight, uses PHP and is easy to use.
Documentation: Template-rendering.

CakePHP Validation cakephp/validation

CakePHP's lightweight validation library makes validation quite easy and straightforward. It has a flexible rule system that supports a wide range of validation rules out of the box. It significantly reduces the complexity of the code compared to an own validation system.
Documentation: Validation.

Symfony Mailer symfony/mailer

Symfony Mailer simplifies email handling. It's practical to use and supports various transports and features like attachment handling.
Documentation: Mailing.

Session handler odan/session

The odan/session library provides a secure and easy-to-use session management solution. It offers protection against session fixation and session hijacking, enhancing the security of the application. It also provides a simple, object-oriented interface for storing and retrieving session data.
Documentation: Session and Flash.

HTTP Messages Interfaces nyholm/psr7

The nyholm/psr7 library provides a straightforward implementation of the PSR-7 HTTP message interfaces. It is one of the fastest PSR-7 implementations and allows for the creation and manipulation of HTTP requests and responses in a standardized way.

Create PSR-7 server request nyholm/psr7-server

This library is a PSR-7 compliant server request creator. It provides a simple and standardized way to create server requests from PHP's superglobals, making it easier to handle incoming HTTP requests in a PSR-7 compliant manner.

HTTP Message Utilities fig/http-message-util

http-message-util is a tiny library that provides constants for referring to request methods, response status codes and messages which makes it a bit easier working with HTTP messages.
Instead of using the HTTP status code number, it allows using the constant name. For example 422 can be replaced with StatusCodeInterface::STATUS_UNAUTHORIZED.

Native PHP extensions

The following native PHP extensions are used:

  • ext-json for JSON encoding and decoding
  • ext-pdo for database access
  • ext-gettext for localization
  • ext-intl for date and time formatting

For them to be enabled, they have to be uncommented in the php.ini file.

Development libraries

The following libraries are only used during development and testing and are not included in the production build.

PHPUnit phpunit/phpunit and helper traits samuelgfeller/test-traits

PHPUnit is the de-facto standard for unit testing in PHP.

The collection of traits samuelgfeller/test-traits (a clone from selective/test-traits with some additional traits and features) contains a lot of useful traits to interact with the database, make HTTP requests, assert emails, mock classes, and more.

Documentation: Test setup and Writing Tests.

Migration generator odan/phinx-migrations-generator

This library generates migration files with the Phinx migrator from an existing database schema.

Documentation: Database Migrations.

PHPStan phpstan/phpstan

PHPStan is a static analysis tool that helps to find bugs in the codebase without having to run the application.

PHP-CS-Fixer friendsofphp/php-cs-fixer

PHP-CS-Fixer is a great tool to automatically fix coding style issues to follow standards.

Clone this wiki locally