I have written a PHP library using the PHP 8.0 readonly
keyword and then I realised that it would be good to support earlier versions of PHP such as 7.4.
I could easily remove the readonly
keywords from my code but I don't want to do that -- they were put there for a reason!
Having a C background, I immediately thought of macros but PHP doesn't seem to have any. I've googled this answer for adding macro-like behaviour to PHP, but it looks like an overkill. And it's just for one file, and my library has 26 files at present.
Is there an easy way to make PHP 7.4 just ignore the readonly
keyword and make my code cross-version? Something to the effect of this C code?
#if PHP_VERSION < 8
#define readonly /**/
#enif
Perphaps some composer
build option that can pre-process files before packaging them up?
most frameworks simply avoid using modern features for this reason alone (WordPress, Symfony, Laravel), but if you insist, your best bet is probably Composer, you can have a v1.x.x with composer.json
and a v2.x.x with composer.json
then when people do
composer require lib
, composer will automatically scan for and install the newest version of your library that is compatible with the local php version and composer.json-constraints (-:the downside is that you'll have to maintain both v1 and v2 of your library for as long as you intend to support php 7.4 though..
another option is to have a loader like
lib.php
again with the downside of having to maintain both lib_modern and lib_legacy