Jack Polgar

Back to (PHP) basics

I've recently been updating some of my PHP based projects to PHP 8.

Some of these projects didn't use any framework, or used a sort of micro-framework. I've found that I enjoy using PHP more without a full-featured framework like Laravel or Symfony.

I ended up creating my own barebones framework that only takes care of routing a request to a controller with middleware and a basic service container. The goal was to create a small, efficient base to build on.

While I build most of my projects these days as an API with a front-end framework. I do occasionally need a templating system, and instead of using an over-engineered templating engine, I figured why not use the best PHP templating system there is, PHP itself but with the ability to extend other files and release it for others.

But "what about a database" you ask. Simple, I like to keep things simple and performant, so I ditched the whole idea of an ORM with a query builder, I don't regularly build my projects so support multiple database systems, I pick one such as PostgreSQL or MariaDB and write plain old SQL queries with PDO.

The most ORM-like my projects get are static classes to run queries and return an object with a few magic functions to access and set data. Essentially User::findById(1); returns an object that uses __call($name) to read from an internal data array, like so: $user->getUsername(); is really just return $this->data[$field] with $field being username from the getUsername() call, other than that it's all plain old prepared SQL statements.