Hacker News new | past | comments | ask | show | jobs | submit login
Run PHP in the browser through Babel (gitlab.com/kornelski)
102 points by tambourine_man on July 26, 2020 | hide | past | favorite | 38 comments



This 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/

Incidentally it's the #3 when you google "Run PHP in Javascript"


"phpjs" and "jsphp" each have their share of results as well


I 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.


Author 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


The 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)


This 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


PHP -> 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


Can 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?


The 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?


There 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.


Dynamic typing is really just passing around variables using an enumerated type. You don't need anything resembling an interpreter to do this and it's how the C extension APIs for many dynamic languages like Ruby and Lua work.


I think people in this thread are using 'dynamic typing' more as a shortcut to also mean features like eval, which does require a full interpreter or compiler.


This might be right, but it’s also true that most Common Lisp program can be compiled to machine code that’s very similar to what a C compiler would produce, except for the occasional runtime type check.


It depends what an interpreter means to you. You might need to use hash table lookups of variables and do switch cases on types in trickier cases where the compiler can't figure out what's going on, but you can leave out the main VM/interpreter part.


Can PHP running in WASM access the DOM? I know there’s a .NET project that does something similar


PHP in Browser has done this: https://oraoto.github.io/pib/.


This 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.


Very 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().


Probably 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.


It's been done, and it rarely works for anything non-trivial.


I 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.


This is so cool. I hate it.


I 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.


I 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/ (2007)


who would have known JavaScript was Greenspun's tenth rule! https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule


This means we can connect to MySQL from the client side! That will be secure, right? ;-)


> 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.


Anyone run WordPress through this yet?


"...your scientists were so preoccupied with whether or not they could, they didn't stop to think if they should."


It'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.


I think modern PHP is worlds better than JS, simply because it has a larger standard library.


I'll take Laravel any day over express/sails/hapi/koa


Highly 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.


[flagged]


"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


As is everything else we do, so we might as well try to have a bit of fun along the way.


When all you have is a hammer ...


This 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.


anything done in php in theory can be done in js. the problem is the browser has to say open a long time




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: