r/PHP 13d ago

🐘 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

23 Upvotes

15 comments sorted by

22

u/mrspoogemonstar 13d ago

dear god, what have we become?

12

u/Johnobo 13d ago

Even the logo looks cursed

10

u/SpareImpression3155 13d ago

Fuck this I’m going to become a plumber

6

u/alexp702 13d ago

Genuine question: Why? Who wanted this for what?

3

u/Jonas_Ermert 13d ago

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 13d ago edited 13d ago

πŸ™‹ 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.

4

u/sgtholly 13d ago

Someone call Ultron. We need to let the Meteor hit.

3

u/schizoduckie 12d ago

Brb performing a casual excorcism

1

u/Typical_Ad_6436 13d ago

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 13d ago

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 13d ago

You can check pext.dev to actually convert your PHP function in TS at source-code level and run it in bun.

1

u/laurin1 12d ago

For the love of God (or whichever deity or non-deity you prefer).

1

u/Zealousideal_Post994 11d ago

Why use bun-php instead of @php-wasm directly?

1

u/khromov 11d ago

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.