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.
atm you have to add `<?php` at the beginning.
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.
> "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.
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.
On the language level most stuff works well enough. However, the biggest blocker is impedance mismatch between PHP and JS environments, e.g. request-per-process vs shared server, `echo`, modules vs autoload & namespaces, etc. so even 100% accurate PHP execution is going to be doing odd things in the JS world.
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.
I dockerized a working version of HPHPc (which is quite difficult to set up due to bitrot) at https://github.com/allanlw/hphpc-docker if you want to play around with the compiler. I might try to take a shot at passing it through emscripten later.
Someone did take roughly the approach you're describing. A VM in JavaScript that groks php bytecode: https://github.com/niklasvh/php.js
It wouldn't be a perfect comparison since the generated JS code might be less optimized. But it could give one a ballpark estimate.
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 ?
0 - http://cretz.github.io/pratphall/ - https://github.com/cretz/pratphall
Many dynamically typed languages treat null/nill/None, empty string, and empty list as "non-truthy" in a Boolean context, but that's not universal.
JavaScript's notion of a class is very different from Python's. Ruby's take on who can call private methods is different from most languages. Python is in a minority for supporting multiple inheritance.
In Common Lisp, functions and variables have separate namespaces, whereas in Scheme, they share a single namespace.
PHP just has bizarre object conversion and comparison rules that are often inconsistent and therefore incompatible with most other languages (ignoring the Turing tarpit).
Getting good enough lexical analysis of the source language to efficiently implement lexical closures in a target language without them can be a pain.
On the other hand, if you're cross-compiling buggy webapps written by developers who don't properly understand the semantics of the language they're written in, maybe you can just ignore the semantic mismatches and get a different set of bugs coming out the other side while accidentally fixing other bugs.
Other than that you're right
Python 2093, C 1630, Go 766, SQL 706, Haskell 691, JavaScript 683, C# 666
I know it doesn't mean anything but I think it's funny, at least.
Full list: https://pastebin.com/3HevKLzz
But I'd like to point out that "the scope of the variables is broken" doesn't make any sense: prior to ES6 JavaScript only had function scope -- which is not broken but also not what most people are used to. ES6 added let/const which are block scoped (and thus more familiar). Neither of the two is broken, nor did the addition break anything.
The only oddity I can think of that would provoke such criticism would be a misunderstanding of non-strict-mode JS's behaviour with regard to implicit globals, e.g. what happens outside strict-mode when variables are used without being declared or what the "with" statement does (which is heavily discouraged). But that's like saying PHP is stupid because its array literals look weird.
Honestly, how can you tell? I feel like that's something someone could say if they are under the misunderstanding you (very aptly) describe.
Luckily ES2015 introduced blocked scoped variables (const and let), so this is no longer an issue.