


Actually, you can choose a carefully defined set of functions to expose to JS. User-generated JS can be safely compiled to PHP because none of the usual PHP features are exposed to JS land (such as disk/network access). One interesting use case might be allowing some user-generated scripting (think plugins) to run in a sandboxed environment on your PHP platform. We're not at a place where you can use this stuff in production, but in theory there's nothing preventing JS compiled to PHP from being viable. Plus, I think there's an unwritten rule that JS must be able to run on every surface in existence. But if you wanted a compile-to-PHP language, that's widely familiar to devs, with plenty of existing tooling, libraries and community, then maybe JS is such a language. So if you need to host on PHP but don't like writing PHP, well in theory you could write in JS and deploy to PHP! That's madness! PHP is ubiquitous in bulk hosting, it runs on millions of servers around the web and PHP applications are generally easy to host and maintain. There are plenty of "compile to JS" languages available (ReasonML, TypeScript, CoffeeScript, Dart), but not many choices in the PHP space **. But it turns out JavaScript and PHP are similar enough to do most everything as a source transformation, and the PHP code produced is still readable. Some amount of code is implemented as runtime helpers, including operations which require type checking and "standard library" features such as Date functions. There are various aspects of the two languages that make this a really interesting challenge: lexical scope, lambdas, prototypal inheritance, PHP's implicit variable declaration, etc.

This is a proof-of-concept and a hobby project, mostly just for fun. If you want to use next-generation JS features you should first pre-process your code using babel. Note: This was a hobby project from a number of years ago and is not actively maintained, however PRs are welcome.Īlso Note: This does not work with ES6+ JavaScript features (which were not widely available at the time this project was written).
