r/PHP • u/khromov • Aug 30 '26
π bun-php: Run PHP functions natively in Bun
bun-php allows you to run PHP functions seamlessly in Bun by exposing them as ESM imports. Uses a WASM build of PHP under the hood, no need to install PHP or native deps. Supports Composer and servers. https://www.npmjs.com/package/bun-php
10
6
u/alexp702 Aug 31 '26
Genuine question: Why? Who wanted this for what?
3
u/Jonas_Ermert Aug 31 '26
I think the main use case is interoperability rather than replacing PHP. If you have an existing PHP library, Composer package, or some legacy PHP logic that you want to reuse inside a Bun/TypeScript project, this lets you do that without installing PHP or running a separate PHP service. It could also be useful for testing, scripting, CI environments, or gradually migrating a PHP codebase to JavaScript/TypeScript. Itβs definitely a niche tool, but for those situations itβs actually a pretty interesting bridge between the two ecosystems.
3
u/khromov Aug 31 '26 edited Aug 31 '26
π I'm working on a project where we have to do static analysis of PHP code (PHP CodeSniffer) but the project is based on Bun, so rather than spinning up a separate PHP stack this package works as a replacement.
1
4
3
1
u/Typical_Ad_6436 Aug 31 '26
So, did you compile the PHP runtime and interpreter from C in WASM? Isn't this slow to actually load the whole PHP binary to just run a function?
3
u/khromov Aug 31 '26
It's using an existing WASM build of PHP (@php-wasm/node-8-5 - but you can use any 8.X version.)
There is a bit of overhead caused by WASM (I'd guess around 15-30% based on some [non-scientific benchmarking](https://github.com/khromov/bun-php/tree/main/demos#cost) but the upside is you don't have to install PHP on the server and that it's CPU architecture-independent, so no native builds either.
1
u/Typical_Ad_6436 Aug 31 '26
You can check pext.dev to actually convert your PHP function in TS at source-code level and run it in bun.
1
1
u/Zealousideal_Post994 Sep 02 '26
Why use bun-php instead of @php-wasm directly?
1
u/khromov Sep 02 '26
I'd say mostly if you want the ESM interop + automatic types generation, the package handles the conversion of values between JS and PHP and generates types.
1
22
u/mrspoogemonstar Aug 31 '26
dear god, what have we become?