Jared McFarland has a recent post to his site showing how to make a PHP extension in Rust, a more recent programming language that seeks to "run fast, prevent almost all crashes and eliminate data races".
Last October I had a discussion with one of my coworkers at Etsy about how writing extensions to interpreted languages like PHP, Ruby or Python should be a lot easier than it is now. We talked a bit about how one of the barriers to successfully writing an extension is they’re generally written in C, and it’s hard to gain confidence in C code if you’re not an expert at the language.
Ever since then I’ve toyed with the idea of writing one in Rust, and for the past few days have been playing around with it. I finally got it to work this morning.
He starts by static his goal, to make a PHP extension with Rust that was compiled into a library along with some C headers. He talks about his use of the Rust Foreign Function Interface to make a "hello world" script and the code to call it from C, complete with header code examples and the result of it being called. He then gets into the PHP integration, showing you the code you'll need for the extension and compiling it into a module and moving it into the extension directory. He also includes some "useless micro-benchmarks" so you can see how things perform, comparing his function to base PHP when calculating the Fibonacci sequence algorithm.