From PHP to Rust: Part I — What is a system language?
By: Italo Baeza Cabrera
Tags: Rust, Software Development, Development, PHP, Programming

PHP have served me right, until now. I have felt that PHP has become something too restrictive for my own projects. I’ll get this straight right away: PHP is not a bad language, is a very useful one specially when paired with frameworks like Symfony and Laravel, but it still lingers behind in features when compared to other languages, like Javascript (Node.JS), Python, Go, or even Rust. No amount of frameworks can fix that.

I decided to jump ship for two reasons: I needed to make something that PHP doesn’t support, and that was microservice-friendly. I don’t think that pulling out thousands of files from the Internet for each Kubernetes Pod can qualify as friendly.

In my case that translated into creating a WebSocket Server. I needed handling video calls between two peers using WebRTC, a protocol that connects both peers directly, while also transferring very important files to the other party through WebSockets to avoid reloading the browser and losing the connection.

PHP wasn’t up to the task. It doesn’t support WebSockets natively, and for WebRTC, you can hack your way in unreliable and resource hogging ways. There is also the problem of not having coroutines, meaning, everything blocks the request-response behaviour.

PHP is nice and all, but I had to dive in and create something from scratch.

That’s where Rust comes in.

Going deep. Going hard.

Rust is a system language. It’s very different to PHP, which is a interpreted language:

A binary “reads” files with PHP code, transform the code in the file to «opcode» into memory, and executes it. Opcache and the next JIT engine in PHP 8 may speed up things. PHP is a web-bound language.

With Rust, on the other hand, you can also do this with some work, but also a graphic engine, a web server (like NGINX), or even a transcoder. There is no limit. And that’s one of the core principles with a system language like Rust: you are the pilot, you just have to know how to drive if you want to go somewhere, anywhere.

Rust for dummies

Most of your coding work that goes into Rust, like you would using C++ or any other similar language, it’s finally compiled into a single binary, which depending on what you do, may take some megabytes. That binary (or “executable”) is bound to the system architecture (x86–64, ARM, RISC-V, for example) and the operative system kernel (Windows, Linux, MacOS, Android, iOS).

You would think that it would require changing the whole code when changing “targets”, but instead Rust comes with a switch that allows you to point where you want to deploy your software, and it supports a whole lot. Unless you’re really doing something very low level, you won’t need to do anything but changing the destination.

The PHP stack on Rust

The “PHP” stack is mostly comprised of having a web-server, like NGINX or Apache, the PHP interpreter running behind of it with a network socket, and the PHP files to read.

Also, you need to get your permissions right, otherwise your application won’t work or your whole server will become vulnerable to attacks.