- This topic has 8 replies, 1 voice, and was last updated 2 months ago by just_another_life.
- November 12, 2020 at 7:25 am #2134leoleolosoParticipant
I’m building a plugin for WordPress. After it was decided WP would stay on PHP 5.6, I thought something had to be done about that. I certainly don’t want to have to wait years to use modern PHP features, and PHP 8 is coming out in barely a few weeks!
So I thought about how Babel compiles JS to make it compatible with older browsers, and I checked if there’s anything of the sort for PHP. And yes, there is: Rector. In its repo it says it’s a tool to modernize code. But it works both ways; after all, it’s just manipulating the AST with PHP-Parser, based on rules that we provide.
I’ve been playing with it for the past few weeks, and I think it’s quite something: I got to add several PHP 7.4 features to my plugin (typed properties, arrow functions), and I transpile it for production, so it can be deployed to PHP 7.1. Soooo cool!
I got accepted to write a couple of articles about it: how transpiling in PHP works, and how to set it up on a project (in my case, applying it to WordPress). Here is the first one:
[An intro to transpiling in PHP via Rector](https://blog.logrocket.com/transpiling-php-code-from-8-0-to-7-x-via-rector/).
(The 2nd one will come out in a few weeks, I’m still working on it)
Check it out. I hope you enjoy it!November 12, 2020 at 7:25 am #2135SavishSalaciousGuest
Wait, WordPress is CHOOSING to stay on 5.6? Are you sure theyre not using more modern PHP? Do you have any links to this content stating other wise or what you stated?November 12, 2020 at 7:25 am #2136satinbroGuest
I’m looking for something similar. Is it possible to transpile down to 5.6?November 12, 2020 at 7:25 am #2137nolokGuest
> However, more likely than not, their absence won’t matter: these features are mostly useful during development to validate the correctness of our code (aided by additional tools, such as PHPUnit for testing and PHPStan for static analysis). If our code has errors and it fails in production, it would fail with or without these new features; at most, the error message will be different.
That is entirely untrue. The transpiling being discussed actively change the behavior of the code and leaves the door massively open to new bugs and security issue.November 12, 2020 at 7:25 am #2138ayeshrajansGuest
This looks like interesting work to nudge the brains. But I think the effort is not worth it.
I would actively downright refuse to work with projects that have such unreasonable runtime requirements. Types, more exceptions, etc have become almost muscle memory and any attempt at downgrading it to 5.6 will be practically dumbing down the code. I infact go for 7.4 minimum for property types.
Second, those who stay at 5.6 are likely the sites abandoned or forgotten. Their host may be still running the service, but it’s unlikely that the site owner will actively make changes to the site, but continue to use 5.6. WordPress core is working with php 7.4.November 12, 2020 at 7:25 am #2139TheKingdutchGuest
This is really cool! Have you thought about making this a composer plugin that processes packages based on the installed PHP versions?
Frameworks such as Drupal would want to adopt new features but can’t due to compatibility. This may provide a solution there by allowing all code to be written as 7.4/8.0 but still run on older 7.x versions! 😀November 12, 2020 at 7:25 am #2140mechluisGuest
5.6? What a shameNovember 12, 2020 at 7:25 am #2141kasnhasnGuest
Stop what you are doing!
The main reason for things like Babel to exist, is that the environment the code runs on is out of your control. This is not true for your own backend services, you can and must update these things.
WordPress will not stay on 5.6. Yes, the core will be kept using only 5.6 features, but nobody is stopping you from writing your own code with the latest and greatest feature set.
If your server can only handle 7.1 talk to your ops and upgrade. 7.1 is no longer maintained and does not even receive security patches any more. It’s a risk running it production.
On the plus side, congratulations on learning rector. It’s an amazing tool and worth knowing!November 12, 2020 at 7:25 am #2142just_another_lifeGuest
I tried this but it apparently there is no rule to downgrade JSON_THROW_ON_ERROR ?
~~And it crashes when I try to downgrade code using union types, do I have to add these rules myself ?~~
Never mind I was just too tired, so I missed this part:
>The remaining transformations we’ll need to unlock access to all new features between PHP 8.0 and PHP 7.0 have [already been documented](https://github.com/rectorphp/rector/issues?q=is%3Aissue+is%3Aopen+%22%5BDowngrade+PHP%5D%22+). Everyone is welcome to contribute to the open-source project and implement any of these rules.
- You must be logged in to reply to this topic.