Babel-preset-php: PHP7 to ES7 syntax translator

  • That's a weekend project and it turned out better than I expected. Thanks to nikic's php-parser, astexplorer.net and Babel infrastructure handling the gnarly bits (e.g. source maps just worked).

    I started it because I hoped it would speed up rewrites of projects from PHP to JS, but now that I've actually seen the results, I realised that bad PHP code makes only worse JS (and there's no point rewriting good PHP code).

    Still, I think it could be useful for sharing some logic between client and the server, without replacing server-side with Node. This isn't a VM, so it has zero overhead at the cost of being loose with semantic edge cases.

    And I find it funny that it now takes 1 line of config to make JS tools work with PHP syntax instead.

  • This is hilarious!

    We ran across @kornelski yesterday asking about some old babel code (1.) and were wondering what the angle is. Kornel's an accomplished coder: he did pngquant and imageoptim.

    The author himself calls it the "stupidest project I've ever done". But it's probably a nice way to get 90% of the way there when converting PHP fns (such as validation et al) into the browser. Also, and perhaps most importantly, it's a great exercise in writing Babel plugins.

    I really enjoyed this. Thanks for writing it, @kornelski.

    1. https://twitter.com/kornelski/status/884485913627295744

  • While there already is a bunch of "why" type comments in this thread, it's worth noting that in the description of the repo it does say:

    > "This project is silly and exists only to expand Atwood's Law."

    So maybe take it with a grain of salt, and just enjoy a neat project for what it is.

  • This isnt running PHP in the browser. It's running a quasi-JS runtime with PHP syntax.

    To run "PHP in the browser" you'd need tests passing on all the major php frameworks, as per, Hack's testsuit. Otherwise you're forcing people to learn some language not quite either JS or PHP.

  • I've generally found the behavior of builtin PHP methods to be less predictable and more involved than in JS, so I wonder if the best approach to writing a transpiler in this case would be to go to the C source and negotiate from there.

    Having done PHP for years and years before the language itself found adequate elegance, I wouldn't want the burden of replicating all its weird behaviors for something like this, at least without seeing the source to understand what's happening.

  • One interesting use case for this would be to benchmark functions. How much faster/slower do various functions perform in PHP vs. NodeJS?

    It wouldn't be a perfect comparison since the generated JS code might be less optimized. But it could give one a ballpark estimate.

  • "The conversion is implemented as an AST to AST translation (with source maps!)"

    Amazing that the code actually reads better than the original. (at least for me)

    I wonder how many of the php extensions can be easily ported to JS code as well ?

  • Heh, I did it the other direction back in the day as a toy [0]. Back in TypeScript's earlier days I figured it could be a neat frontend for the otherwise ugly PHP...not really.

    0 - http://cretz.github.io/pratphall/ - https://github.com/cretz/pratphall

  • This is a good scary story for anyone who complains too much about the complexity of modern javascript.

  • Why not just compile phplib using https://github.com/kripken/emscripten and call the library functions directly?

  • I could see many ways how this can go wrong and be popular with guys who have PHP code and want to run it on the browser because they 1. either don't master javascript or 2. want to save time.

  • I wonder how easy it is to translate between dynamic languages since they all support a quite similar core set (classes, closures, sets, dicts, iterables ...)

  • But...but...why?

  • Gitlab is down

  • Why?

  • Ah, now we can run HN's least favorite language on top of HN's second least favorite language.

  • Why not? JSX paved the way for the successful return to PHP3 style development.

  • So sick! /o\

  • Fine but its dangerous. Javascript/Ecmascript, is a poor designed language, the scope of the variables is broken. Instead, PHP is well defined in this aspect.