6 ms·
Run PHP in the browser through Babel
- hu3 6y agoI like the description: > Babel preset for converting PHP syntax to JavaScript. It can run subset of PHP in the browser or in Node.js. Expanding Atwood's Law.
- hadrien01 6y agoI didn't know Atwood's Law, so there it is: > Any application that can be written in JavaScript, will eventually be written in JavaScript. https://blog.codinghorror.com/the-principle-of-least-power/ https://blog.codinghorror.com/the-principle-of-least-power/ (2007)
- cheez 6y agowho would have known JavaScript was Greenspun's tenth rule! https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule
- franciscop 6y agoThis is cool, it's a tool to transpile it at build time so you are two steps removed from running PHP on the browser. I dissected this tool a while back and built another that lets you write and run PHP straight in the browser by doing the transformation on-the-fly. Wrote a blog post and all around it: https://francisco.io/blog/running-php-in-javascript/ https://francisco.io/blog/running-php-in-javascript/ Incidentally it's the #3 when you google "Run PHP in Javascript"
- pluc 6y ago"phpjs" and "jsphp" each have their share of results as well
- arkanciscan 6y agoThis is an impressive waste of time
- dang 6y ago"Please don't post shallow dismissals, especially of other people's work. A good critical comment teaches us something." https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- arkanciscan 6y agoNah
- ZephyrBlu 6y agoAs is everything else we do, so we might as well try to have a bit of fun along the way.
- EE84M3i 6y agoThis is cool. I wonder if anyone has compiled the php interpreter via emscripten? Another option might be compiling HPHPc, which I dockerized a few years back: https://github.com/allanlw/hphpc-docker https://github.com/allanlw/hphpc-docker
- err4nt 6y agoPHP -> WASM, can run anywhere WebAssembly runs: which is all modern browsers, embeddable into any language with a WASM runtime for it, or usable with a standalone WASM runtime: https://wapm.io/package/php https://wapm.io/package/php
- Nextgrid 6y agoCan you realistically compile an interpreted language into WASM without embedding an interpreter? If I remember right, PHP has “eval”, so how would WASM code deal with such a statement (that could evaluate any possible code) without embedding a PHP interpreter?
- TazeTSchnitzel 6y agoThe comment you replied to linked to a WebAssembly build of the PHP interpreter. But to answer your question, there's no fundamental difference between interpreted and compiled languages. If the program you want to compile doesn't use `eval`, and you can statically determine all the files it would include, why couldn't you compile it without including an interpreter?
- nicoburns 6y agoThere is a fundamental difference between dynamically typed and statically typed languages though. And if you want to AOT compile a dynamically typed language, then you're going to need to include code that looks awfully like an interpreter in the final binary.
- 6y ago
- DJBunnies 6y agoIt's been done, and it rarely works for anything non-trivial.
- 0x0 6y agoI mean, it looks like their version of PHP arrays are very different, even "$a = $b" doesn't copy the the array, just references the same. I think anyone using this expecting to write PHP is in for some subtle and not so subtle surprises.
- neovive 6y agoVery nice! It's interesting that some of the new features released in PHP 7.4 and 8 have direct parallels in JS. Here are a few examples: arrow functions, spread operator in arrays, weak maps, str_starts_with(), str_ends_with().
- pests 6y agoProbably a direct result of cross pollination between the communities. Since PHP was historically embedded into HTML you would get a lot of front end technology exposure.
- ecmascript 6y agoThis is icredible impressive, I have always thought php probably could be translated to js. I could definately see myself using this for future php projects. Although, I mostly do javascript today anyway.
- paulpauper 6y agoanything done in php in theory can be done in js. the problem is the browser has to say open a long time
- deleted 6y ago[deleted]
- laurensr 6y agoThis means we can connect to MySQL from the client side! That will be secure, right? ;-)
- aikah 6y ago> This means we can connect to MySQL from the client side! That will be secure, right? ;-) Not really, the MySQL server would need to support something such as webSocket or WebRTC to be able to directly connect to a browser (or just plain HTTP). So you'd still need either to modify MySQL or write a proxy server.
- wh-uws 6y ago"...your scientists were so preoccupied with whether or not they could, they didn't stop to think if they should."
- na85 6y agoIt's definitely fashionable to hate on PHP but its modern incarnation has no more warts than JavaScript and the two languages are probably abused by developers at about the same rate. Still, it would have been neat to see this headline read "Run Scheme in the browser through Babel" or similar.
- phendrenad2 6y agoI think modern PHP is worlds better than JS, simply because it has a larger standard library.
- dimitrios1 6y agoI'll take Laravel any day over express/sails/hapi/koa
- nwmcsween 6y agoHighly highly subjective, js is tiny compared to PHP so you have all the issues with what_some_one wanted toName a function vs the standard PHP way. Not to mention js is very composable compared to most PHP.
- deleted 6y ago[deleted]
- TazeTSchnitzel 6y agoI think “run PHP in the browser” is overstating what this does, it's a naïve source-to-source translator and will not run any non-trivial PHP application, because JS and PHP have very different semantics. There are more sophisticated existing projects that actually try to compile PHP code or the PHP interpreter for the browser.
- pornel 6y agoAuthor here. Yes, it's an approximate conversion favoring readable output over exact semantics, so it won't run WordPress. It might still be useful for smaller pieces of code. If you want to rewrite a PHP program in JS (which may be a bad idea…), it will do the most tedious step for you. I wrote it just as a proof of concept. It was a warm-up before writing a similar tool for C to Rust source-to-source translation: https://lib.rs/citrus https://lib.rs/citrus
- 1337shadow 6y agoThe point is to export some functions I suppose, ie. code server side form validation and have it converted to frontend code too (assuming it doesn't need to hit the database)
- J5892 6y agoThis is so cool. I hate it.
- aronpye 6y agoWhen all you have is a hammer ...
- joemaller1 6y agoAnyone run WordPress through this yet?
- deleted 6y ago[deleted]
- bkanber 6y agoThis right here is what the internet should be about. Stop debating whether this makes sense to do. It doesn't, and it doesn't have to. It's an absurd academic pursuit, and that makes the world a slightly better and more interesting place.