Posts about PHP
-
Do Not Use Exception Class Directly
It is a bad practice to use the
Exception
class directly in PHP. That means 2 points:Do not throw
Exception
directlyDo not catch
Exception
directly
Subclassing
Exception
is not a direct use in this context.Do not throw
Exception
There are a lot of specific
Exception
descendants in PHP. See documentation for the full list. It's not necessary to use and remember all of them, many have unclear use cases, but at very least you should separate your exceptions intoRuntimeException
andLogicException
. As the doc says,LogicException
means code misuse. ThrowLogicException
in cases when the upstream must be fixed because it uses your code incorrectly. ThrowRuntimeException
in cases that are not predictable by the upstream, like service failures or invalid user input.In case you use exceptions for flow control, you need your own custom exceptions anyway. In this case you can extend the base
Exception
, extendingException
directly is not a sin, especially if your exceptions are not error conditions.Do not catch
Exception
Assuming you follow the previous point, there is no need to catch
Exception
's.If you don't guard against a specific situation, catch all
RuntimeException
's.LogicException
's should go to your logger.In a global error handler that writes your logs, you need to catch Errors too, so you need to go even further and catch
Throwable
.If you use exceptions for flow control (please don't haha) you already catching your own exceptions, and if you follow the previous point, they are already separated from actual error conditions and won't be caught mistakenly by a wrong level.
Notable exceptions to the rule:
You use PHP below 7.0 and don't have
Throwable
. Well, it's 2024 already so this is another call for you to upgrade. Otherwise assume point 2 allows you to catch a baseException
.You use a library that throws plain
Exception
instances. Well, bad for you, but maybe it's a good idea to report this thing as a problem.
-
De-hybridize Torrent
A feature that may be desired for some anti-V2 trackers. While I would like to see the torrent community going the other way around, some trackers choose not to allow version 2 torrents and sometimes even hybrids.
The torrent file library now allows cleaning unnecessary or undesired metadata versions from a torrent file.
The intended use and the primary future scope is actually the other way around: the ability to remove version 1 metadata to make use of easy mutability of version 2 torrents to add or remove files in existing torrent structures.
-
PHP Requirements on Packagist
A followup for the previous post: the minimum required PHP versions in packages on the Packagist.
-
How to Choose a Minimum PHP Requirement
As a library developer I often face a problem what should be a minimum PHP requirement. Here are some of my principles.
-
UUID Library (and ULID to UUID migration)
I created a UUID and ULID library with Doctrine support. Well, who didn't :D
The main scenario for myself was a possible migration for a project from ULIDs to UUIDs since UUIDs got a little bit of support in MySQL 8.
Example code:
<?php use Arokettu\Uuid\UuidParser; // Just a random ULID $ulid = UuidParser::fromBase32('01H44RDYXJPFCF895N3BBXCZRC'); var_dump($ulid->isUuidV7Compatible()); // false // UnexpectedValueException: This ULID cannot be converted to UUID v7 losslessly // $uuid = $ulid->toUuidV7(); $uuid = $ulid->toUuidV7(lossy: true); // note digit 13 becoming '7' and digit 17 moving into [89ab] range var_dump($uuid->toString()); // 01890986-fbb2-73d8-b424-b51ad7d67f0c var_dump($ulid->toRfc4122()); // 01890986-fbb2-b3d8-f424-b51ad7d67f0c var_dump($uuid->toBase32()); // 01H44RDYXJEFCB895N3BBXCZRC var_dump($ulid->toString()); // 01H44RDYXJPFCF895N3BBXCZRC
-
March Packages on Packagist
I'm continuing to publish useless nih junk on the Packagist:
arokettu/arithmetic-parser: A very configurable arithmetic parser and calculator. You can define your own set of functions and operators. You can even disable multiplication if you want to!
arokettu/clock: A set of PSR-20 clock implementations. Everyone has these implemented so why not me? :D
arokettu/system-clock: All these PSR-20 clock packages usually contain a ton of clocks for testing purposes but most of the production code needs only
new DateTimeImmutable('now')
so I extracted SystemClock to a separate package that has only SystemClock.php and composer.json for the smallest possible package (if you don't count uglifying it js-like)-
arokettu/kilo-mega: This one was actually released on February but I've forgot to write about it. It's a simple formatter for metric prefixes.
Also a new significant release:
arokettu/torrent-file: Torrent file class got file listing classes for v1 and v2 metadata in 5.0 release.
-
Moving packages to Arokettu
Just finished moving my old composer packages from
sandfoxme/*
toarokettu/*
.Full list:
I tried to make the migration process as smooth as possible. I'm sorry for the inconvenience it might have caused.
-
Installing Composer in Docker the 2020-s Way
While the original way is still valid, in Docker it's now easier to copy the Composer executable from its Docker image.
or for PHP < 7.2.5
Please note that without the installer, dependencies won't be checked, so you also need to install git and/or zip, and preferably optional dependencies like mbstring too.
Found accidentally on the Stack Overflow. There is also another interesting and underliked method for the case when you don't want to keep the composer executable and cache files in your image.
-
OpenSpout
I have recently found myself in a situation when 3 out of 4 libraries that I used to work with Excel files were abandoned:
akeneo-labs/spreadsheet-parser (streaming reader) had no commits in 4 years
mk-j/php_xlsxwriter (streaming writer) had no commits in 2 years
box/spout (streaming reader/writer) is formally abandoned
Luckily there exists a community fork of Spout called OpenSpout and it can replace them all. It seems pretty popular with 1 mln downloads and 229 stars but it's still less popular than the original and I managed to find it only by navigating the forks of Spout so I think it just needs more exposure.
-
PHP 8.2 and my achievement
A small step for the community may still be a huge leap for specific people, like Pierrick Charron and Sergey Panteleev who became this version release managers. I would like to congratulate them with a successful release.
This moon-in-reverse scale leap also happened to me: I got my first MR accepted to the PHP itself. It's the second C-based project that I contributed professionally, and the only huge one. The first one was my systemd-journal lib wrapper.