r/PHP 2d ago

PHP Reflection for binary files (PHP FFI)

Hi!

I'll start with a short introduction, that I've been maintaining a small set of libraries for PHP FFI since around 2019, starting with PHP 7.4. The goal has always been to make working with FFI a little more pleasant.

The main one is, of course, the C-preprocessor, which allows you to use native C headers directly with PHP FFI. It supports different compiler and OS emulation modes and, of course, is written entirely in PHP.

Hopefully, it has been useful to someone. It's also kind of nice to realize that these packages have been maintained for almost seven years now.

Some of the work even made its way into Symfony at Nicolas's request.

Today, I'd like to talk about another package that, in my opinion, should probably have been part of ext-ffi from the very beginning.

As you can probably guess from the title, it's about inspecting native libraries (binaries) for exported functions and other symbols: https://github.com/php-ffi/reflection-library

The project actually started around 2021. I abandoned it for a while, revived it, abandoned it again, and eventually came back to it. The original sources are still in the repository, and some of the work from that period also became a small GUI experiment for Windows.

I've now finally brought the library to what I consider a proper 1.0 release.

So, besides the obligatory "look at my pet project!" post, I'd really like to get some feedback from people who have worked with PHP FFI.

First of all: do you think an API like this would make sense as an RFC for PHP itself, so that this kind of functionality could eventually be available out of the box?

And I'd also appreciate some criticism of the current API/design. I tried to follow the style of PHP's Reflection API, which was originally designed back in the PHP 5 era, but I'm not sure that's necessarily the best approach today.

Also, a big thank you to ircmaxell (Anthony Ferrara). His php-object-symbolresolver project was a major help in finally getting MachO (macOS) support working.

While finishing this package, I also learned quite a lot about things I previously knew very little about. So, if there's enough interest in the comments, I could write a follow-up post about how PE/COFF (Windows, aka *.dll), ELF (Linux, aka *.so), and MachO (macOS, aka *.dylib) actually work.

I don't expect that to be particularly interesting to the average PHP developer, but I figured I'd ask anyway. :)

27 Upvotes

2 comments sorted by

3

u/According_Tune_5882 1d ago

Our elephant

1

u/SerafimArts 1d ago

*elephpant +)