Menu

React, etc. Tech Stack

React, Flux, GraphQL, Hack, HHVM...? All of this and more!

Run Express.js written in PHP on Node

Running code written in PHP is now possible due to Kornel Lesiński's Babel Preset PHP project.

While this is not recommended for production and started as a joke, it is now feasible to execute PHP code in Node.js as well as in the browser.

In addition to purely fictional examples, you can also execute popular Node.js frameworks now using PHP syntax, since essentially you are writing code that is transpiled to plain JavaScript.

For example, there is a skeleton project for starting Express.js implementations based on PHP syntax:

<?php
$express = require('express');
$app = $express();
$app->use($express->static('dist'));

$app->get('/', function($req, $res) {
  $res->send('Hello world!');
});

$app->listen(3000, function () {
  $console->log('Example app listening on port 3000!');
});

The obviously looks a lot like what a Silex application would look like, which in turn is heavy influenced by the Sinatra microframework written in Ruby.

The underlying implementation relies on an AST to AST translation and produces valid JavaScript from PHP source code, with certain feature limitations that come from the fundamental low-level differences between the languages.

While this is now feasible, it is not recommended... for obvious reasons. Still, it is an impresive feat and checking the babel-php-preset project source code is a good exercise for anyone looking into programming language development and tooling. For native PHP, the work on PHP JIT implementation continues.

Written by Jorgé on Monday July 17, 2017

Permalink -

« TypeScript interest peaks in Google Trends' July 2017 stats - Functional Programming with TypeScript »