Joseph Edmonds

MicroDeps – A New PHP Concept

by Joseph Edmonds

by Joseph Edmonds

The PHP ecosystem benefits from a wide range of really great libraries and frameworks. I strongly encourage people to use these. However, some times you just want to keep it lean and mean. For these kinds of times, you probably just think to roll your own solution from scratch, however that does see you writing a lot of boilerplate code.

MicroDeps

So I am proposing a solution, a middle ground. MicroDeps will be very small libraries that aim not to be abstraction layers or wrappers, but instead they will be small utilites that make working with raw PHP a little bit easier and more productive.

I’ve kicked this off with the first MicroDep library – MicroDeps/PDO

This is not a PDO wrapper! It is simply a set of factories and configuration management to assist with getting a working PDO instance with some sane configuration. The PDO you will get by using it is plain old PDO.

I’ve written it with all the clean code paradigms I can muster, used the best static analysis tools at their most brutally strict settings and ensured that test coverage is 100% and that it even withstands mutation testing.

This is an alternative to something like Doctrine/DBAL which aims to be a much more robust and extensive wrapper with a lot of functionality.

So why would you use a MicroDep?

Well firstly, I think you might want to look at them so that you can learn a bit more about PHP internals, rather than having everything abstracted away and learning userland library and framework code.

Secondly, you might just want to keep things really simple – perhaps you are just spinning up a simple script.

Thirdly, you might be wanting to keep things as lean as possible for performance reasons

Other Ideas

One of my ideas with this would be to actually encourage you to copy/paste the micro dep straight into your normal codebase rather than installing it as a composer dependency. I know, it’s crazy right?

Composer and dependencies are great, but some times they come with extra pain that can be hard to manage over time. By reducing the number of dependencies you have in play, you may have an easier time in the long run as all your code is first party and tracked in your main repo.

I am considering writing some tooling to assist with bringing micro deps into your main codebase, though I’m not sure it’s even a good idea or just one of my left field “out of the box” thought tangents.

PHP QA CI

To assist with writing the this MicroDep, I have used another library called PHP QA CI. This is actually a tool that was originally developed for Edmonds Commerce, though it doesn’t really work well with Magento 2 which is the main focus there and so has languished a bit. I wanted to get it bang up to date with the latest versions of the various tools that are bundled and also really test it on a PHP 8 project.

I did use the Edmonds Commerce version to help writing The Art of Modern PHP 8 however the PHP 8 support was a little lacking. I’ve now created a fork of the original and will be working on it and making it work really nicely with PHP 8 and 8.1 when that comes out. As this might be a long running fork, I have registered it on Packagist in it’s own right.

Hope its Useful

I like to share stuff online and I really hope it’s useful to someone else. Feel free to drop me a note, connect on linkedin, message me on Reddit with any questions or comments 🙂

Facebook
Twitter
LinkedIn
About Joseph Edmonds

About Joseph Edmonds

Personal musings, tech tips, trivia and other things that might be interesting.

Get in touch today

Send me a message concerning your idea, problem, or question – and I’ll personally respond as soon as possible.

Connect with me on LinkedIn.