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.
Indeed. For this I prioritized readable and JS-like output (and no runtime) over handling things exactly.
There's a different project doing it via a VM in JS: https://phpjs.hertzen.com/ and it handles most of the PHP test suite.
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.
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.
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.
Clue 1: it praises PHP. Even PHP apologists usually restrict themselves to simply defending PHP rather than praising it.
Clue 2: it calls JS "dangerous" and "poor designed" (sic). This is vague but provocative enough to invite emotional responses rather than factual discussion.
Clue 3: it explicitly calls out "the scope of variables" as "broken". Again this is vague enough to be ambiguous but strongly worded enough to seem insulting. This is especially confusing if contrasted with PHP, which isn't exactly known for being intuitive and simple in that regard: http://php.net/manual/en/language.variables.variable.php
But you're right, which is why I said it seems like an obvious troll comment rather than insisting that it can only be that. It might just be a poorly worded expression of frustration stemming from an incomplete understanding. Hence my attempt at a factual response.
I don't really think anyome ought to be starting flame wars over whether PHP or JS is a poorly designed language. It's like watching Hitler fight Stalin.
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.
I think the difference is that PHP's built-ins were originally created as a "toolset" and thus often do more than one thing in fairly specific combinations. I believe PHP has improved on that but it can still get hairy because of the sheer number of functions living in the global namespace (and horribly outdated community-provided examples in the documentation).
If you want to use an existing toolchain, you could use HPHPc (the predecessor to HHVM, which transpiled PHP to C code) and emscripten to translate from C to Javascript. It would probably require some mucking around at the emscripten layer though to get all the packages linking properly, because HPHPc has a non-trivial number of (non-optional) runtime deps for supporting all of the various php modules. You should just be able to compile them separately with emscripten into a prefix (e.g. don't use emscripten ports, which I've had a terrible time with) but YMMV.
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.
I feel you, brother. There are too many people using crappy languages. If only everybody saw the light and used the only good language out there, Oberon-2.
I feel your sarcasm. Lets be real though, V8 is a fast implementation but javascript as a language itself separate from implementation is a POS. ES6 and ES7 is proof that ES5 was just plain bad.
Wrong. Javascript was designed in a week. The creator admits it's bad. This is universally known by professional programmers, why are people defending this?
Everyone's saying that JS is bad, it's been proven, big league, the most expert programmers use Oberon-2, and believe me that's what we've got here only the most expert legitimate programmers, getting tired of winning.
Are you serious? Please take a look at this message:
>Wrong. Javascript was designed in a week. The creator admits it's bad. This is universally known by professional programmers, why are people defending this?
That is an opinionated message about a language. I imagine people are taking offense to my opinion, in which case I urge everyone to take a step back and look at the difference between the insulting sarcasm and an extreme opinion. Sometimes circumstance and bias blinds you into taking offense, especially if you're one of those people who only knows javascript.
>Should I not consider myself a professional developer because I don't find JavaScript bad? In fact, there are parts of it I quite like.
This message illustrates exactly what I'm talking about. When did I say any of this? Did I say you're not a professional developer because you don't find javascript bad? No. I said Professional developers find javascript to be really shitty, and it's quite universally known. My mistake, I should have wrote "Many" professional developers, as you seem to mistook it to be "All" professional developers.
Look, there is no denying this fact. A great number of really good developers intensely hate javascript, there's a reason why. I will say this: You are a horrible javascript developer if you do not have the ability to comprehend the bad parts of javascript and why "many" developers find the language so shitty.
> That is an opinionated message about a language.
No. It's a rude, factually incorrect, and just plain obnoxious comment with nothing to recommend it. Are you shocked it was met with ridicule and hostility?
> you seem to mistook it to be "All" professional developers.
That's because you're a bad writer who speaks in (incorrect) absolutes.
> Look, there is no denying this fact.
He said while lots of people deny it.
> A great number of really good developers intensely hate javascript, there's a reason why.
I've never found a really good developer to hate any language. Good developers tend to appreciate languages for their strengths - even if they prefer not to use them or don't like their idioms.
It's often a sign of a very poor developer to get into the kind of crazy language war stuff you're talking about.
>No. It's a rude, factually incorrect, and just plain obnoxious comment with nothing to recommend it. Are you shocked it was met with ridicule and hostility?
I'm never shocked it when things are met with hostility. It's natural for people to meet constructive criticism with hostility, that is human nature and apparently yours as well. What I said was not rude at all, I directly re-quoted what I said so you can point out exactly which part of my message was rude. You couldn't. You are a liar, which is a thousand times worse than rude. Also it's not factually incorrect. It's factually correct. Stop lying.
>> you seem to mistook it to be "All" professional developers.
>That's because you're a bad writer who speaks in (incorrect) absolutes.
I am a bad writer or are you a bad reader? Where exactly did I use the term "All" developers? Nowhere. If I used that term nowhere than logically there is no way I could have spoken in absolute terms. You are again lying. You are a liar. The worst of it's kind. I wonder if you are just not reading the text carefully or if you are deliberately just an ass.
>I've never found a really good developer to hate any language. Good developers tend to appreciate languages for their strengths - even if they prefer not to use them or don't like their idioms.
>It's often a sign of a very poor developer to get into the kind of crazy language war stuff you're talking about.
You haven't met enough developers. Good developers tend to be highly, highly opinionated. They have extreme opinions about many things and languages they hate. Again you are speaking from inexperience or lies. Lets put it this way: It is not probable that all languages are created equal... with strengths balancing weaknesses, this is not the way the world works. Things come out better or worse, rarely are things created totally yin yang balanced... Languages are the same... some are better some are worse.
>> Look, there is no denying this fact.
>He said while lots of people deny it.
You gotta be kidding me. There are a good amount of developers who hate javascript. Unless you don't work at a node shop than you've probably been drinking that koolaid since forever. Many, Many developers don't like javascript, this is obvious af.
You are being rude and offensive and using sarcasm to insult my intelligence. If I am ever rude it's in response to your rudeness. You and I are both biased since we are participating in a hostile argument. When the third party says you're rude you're most likely being rude. Don't let bias blind you to your own actions, look at what I wrote and look at what you wrote and think carefully on whether my opinion on javascript is insulting to the language or to you personally... Than examine your sarcasm... is that a direct insult to me, or an opinion on a topic... I think the answer is fairly obvious and the answer is... you are the one being rude.
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.
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.
So you can tell me why it isn't, then? I don't see WPF being as clean as React is. It uses tons of magic strings in XAML files. JSX properties are expressions and thus can be statically typed. QML is almost the same, just a little different syntax, but I don't see any difference that would make it cleaner than React.
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.
You did a great job with this, I started something really similar about 6 months ago because I wanted to port SabreDAV to js (I didn't quite get to the stage of self hosting PHP parser) but eventually came to the same conclusions, even with a good codebase idiomatic PHP does not map cleanly to idiomatic JS
HN has two very divergent opinions on JS but the people who hate it really hate it,and the people who like JS may outnumber the ones who hate it but you can reliably expect people to complain about Javascript on every HN thread about it. JS is third in dislikes on there (Java is actually the second-most hated language, who knew?), so I wasn't technically correct but I was in the ballpark.
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.
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 ...)
The semantics can still be quite different. You start hitting a lot of corner cases very quickly, unless one of the languages was designed to have semantics close to the other's, or the two languages at least share heavy influence from a common ancestor.
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.
65 comments
[ 3.2 ms ] story [ 132 ms ] threadTo 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.
Luckily ES2015 introduced blocked scoped variables (const and let), so this is no longer an issue.
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.
https://en.wikipedia.org/wiki/Poe%27s_law
Clue 2: it calls JS "dangerous" and "poor designed" (sic). This is vague but provocative enough to invite emotional responses rather than factual discussion.
Clue 3: it explicitly calls out "the scope of variables" as "broken". Again this is vague enough to be ambiguous but strongly worded enough to seem insulting. This is especially confusing if contrasted with PHP, which isn't exactly known for being intuitive and simple in that regard: http://php.net/manual/en/language.variables.variable.php
But you're right, which is why I said it seems like an obvious troll comment rather than insisting that it can only be that. It might just be a poorly worded expression of frustration stemming from an incomplete understanding. Hence my attempt at a factual response.
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.
Someone did take roughly the approach you're describing. A VM in JavaScript that groks php bytecode: https://github.com/niklasvh/php.js
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.
Right? Because otherwise you’d be making no sense, and you’d not do that, would you?
Should I not consider myself a professional developer because I don't find JavaScript bad? In fact, there are parts of it I quite like.
>Wrong. Javascript was designed in a week. The creator admits it's bad. This is universally known by professional programmers, why are people defending this?
That is an opinionated message about a language. I imagine people are taking offense to my opinion, in which case I urge everyone to take a step back and look at the difference between the insulting sarcasm and an extreme opinion. Sometimes circumstance and bias blinds you into taking offense, especially if you're one of those people who only knows javascript.
>Should I not consider myself a professional developer because I don't find JavaScript bad? In fact, there are parts of it I quite like.
This message illustrates exactly what I'm talking about. When did I say any of this? Did I say you're not a professional developer because you don't find javascript bad? No. I said Professional developers find javascript to be really shitty, and it's quite universally known. My mistake, I should have wrote "Many" professional developers, as you seem to mistook it to be "All" professional developers.
Look, there is no denying this fact. A great number of really good developers intensely hate javascript, there's a reason why. I will say this: You are a horrible javascript developer if you do not have the ability to comprehend the bad parts of javascript and why "many" developers find the language so shitty.
No. It's a rude, factually incorrect, and just plain obnoxious comment with nothing to recommend it. Are you shocked it was met with ridicule and hostility?
> you seem to mistook it to be "All" professional developers.
That's because you're a bad writer who speaks in (incorrect) absolutes.
> Look, there is no denying this fact.
He said while lots of people deny it.
> A great number of really good developers intensely hate javascript, there's a reason why.
I've never found a really good developer to hate any language. Good developers tend to appreciate languages for their strengths - even if they prefer not to use them or don't like their idioms.
It's often a sign of a very poor developer to get into the kind of crazy language war stuff you're talking about.
I'm never shocked it when things are met with hostility. It's natural for people to meet constructive criticism with hostility, that is human nature and apparently yours as well. What I said was not rude at all, I directly re-quoted what I said so you can point out exactly which part of my message was rude. You couldn't. You are a liar, which is a thousand times worse than rude. Also it's not factually incorrect. It's factually correct. Stop lying.
>> you seem to mistook it to be "All" professional developers. >That's because you're a bad writer who speaks in (incorrect) absolutes.
I am a bad writer or are you a bad reader? Where exactly did I use the term "All" developers? Nowhere. If I used that term nowhere than logically there is no way I could have spoken in absolute terms. You are again lying. You are a liar. The worst of it's kind. I wonder if you are just not reading the text carefully or if you are deliberately just an ass.
>I've never found a really good developer to hate any language. Good developers tend to appreciate languages for their strengths - even if they prefer not to use them or don't like their idioms. >It's often a sign of a very poor developer to get into the kind of crazy language war stuff you're talking about.
You haven't met enough developers. Good developers tend to be highly, highly opinionated. They have extreme opinions about many things and languages they hate. Again you are speaking from inexperience or lies. Lets put it this way: It is not probable that all languages are created equal... with strengths balancing weaknesses, this is not the way the world works. Things come out better or worse, rarely are things created totally yin yang balanced... Languages are the same... some are better some are worse.
>> Look, there is no denying this fact. >He said while lots of people deny it.
You gotta be kidding me. There are a good amount of developers who hate javascript. Unless you don't work at a node shop than you've probably been drinking that koolaid since forever. Many, Many developers don't like javascript, this is obvious af.
Right? Because otherwise you’d be making no sense, and you’d not do that, would you?
> "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.
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
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 ?
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.
It wouldn't be a perfect comparison since the generated JS code might be less optimized. But it could give one a ballpark estimate.
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
[1]: https://news.ycombinator.com/item?id=6527104
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