DEV Community

Javad
Javad

Posted on

How can I bind OLSRT to PHP?

Hey Dev Community!

After my last blog about binding OLSRT to Node.js, I’m back again—this time with a new challenge: How can I bind OLSRT to PHP?

If you’ve ever wanted to bring async/event‑driven power into PHP, this post is for you. Let’s dive in together!


🔧 Prerequisites

Before we start, make sure you have:

  • Knowledge: C programming and basic PHP internals (Zend API).
  • Tools:
    • On Windows: PHP SDK, Visual Studio (MSVC), Git.
    • On Linux/macOS: php-dev, phpize, GCC/Clang, Autotools, Git.
  • OLSRT repo cloned:
  git clone https://github.com/OverLab-Group/OLSRT
Enter fullscreen mode Exit fullscreen mode

📂 Project Setup

Inside your project root, create:

  • php_olsrt.h — header file
  • php_olsrt.c — main binding code
  • config.m4 (Linux/macOS) or config.w32 (Windows) — build config
  • test.php — to test the extension

Also add include directories to compiler: OLSRT/src, OLSRT/includes, and Root Project.


📝 php_olsrt.h

#ifndef PHP_OLSRT_H
#define PHP_OLSRT_H

extern zend_module_entry olsrt_module_entry;
#define phpext_olsrt_ptr &olsrt_module_entry

#define PHP_OLSRT_EXTNAME "olsrt"
#define PHP_OLSRT_VERSION "0.1.0"

PHP_FUNCTION(olsrt_hello);
PHP_FUNCTION(olsrt_version);

#endif /* PHP_OLSRT_H */
Enter fullscreen mode Exit fullscreen mode

📝 php_olsrt.c

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_olsrt.h"
// #include "olsrt.h" // Uncomment when OLSRT headers are ready

PHP_FUNCTION(olsrt_hello)
{
    php_printf("[OLSRT] Hello from PHP binding!\\n");
    RETURN_TRUE;
}

PHP_FUNCTION(olsrt_version)
{
    RETURN_STRING("OLSRT PHP binding 0.1.0 (stub)");
}

static const zend_function_entry olsrt_functions[] = {
    PHP_FE(olsrt_hello, NULL)
    PHP_FE(olsrt_version, NULL)
    PHP_FE_END
};

PHP_MINIT_FUNCTION(olsrt) { return SUCCESS; }
PHP_MSHUTDOWN_FUNCTION(olsrt) { return SUCCESS; }
PHP_RINIT_FUNCTION(olsrt) { return SUCCESS; }
PHP_RSHUTDOWN_FUNCTION(olsrt) { return SUCCESS; }

PHP_MINFO_FUNCTION(olsrt)
{
    php_info_print_table_start();
    php_info_print_table_header(2, "OLSRT support", "enabled");
    php_info_print_table_row(2, "Version", PHP_OLSRT_VERSION);
    php_info_print_table_end();
}

zend_module_entry olsrt_module_entry = {
    STANDARD_MODULE_HEADER,
    PHP_OLSRT_EXTNAME,
    olsrt_functions,
    PHP_MINIT(olsrt),
    PHP_MSHUTDOWN(olsrt),
    PHP_RINIT(olsrt),
    PHP_RSHUTDOWN(olsrt),
    PHP_MINFO(olsrt),
    PHP_OLSRT_VERSION,
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_OLSRT
ZEND_GET_MODULE(olsrt)
#endif
Enter fullscreen mode Exit fullscreen mode

⚙️ config.m4 (Linux/macOS)

PHP_ARG_ENABLE(olsrt, whether to enable OLSRT support,
[  --enable-olsrt   Enable OLSRT support])

if test "$PHP_OLSRT" != "no"; then
  PHP_NEW_EXTENSION(olsrt, php_olsrt.c, $ext_shared)
  PHP_ADD_INCLUDE(/absolute/path/to/OLSRT/includes)
fi
Enter fullscreen mode Exit fullscreen mode

⚙️ config.w32 (Windows)

ARG_ENABLE("olsrt", "Enable OLSRT support", "no");

if (PHP_OLSRT != "no") {
    var OLSRT_INC = "C:\\path\\to\\OLSRT\\includes";
    ADD_SOURCES(configure_module_dirname, "php_olsrt.c", "olsrt");
    ADD_INCLUDE(OLSRT_INC);
}
Enter fullscreen mode Exit fullscreen mode

▶️ Build & Install

Linux/macOS

phpize
./configure --enable-olsrt
make
sudo make install
Enter fullscreen mode Exit fullscreen mode

Add to php.ini:

extension=olsrt.so
Enter fullscreen mode Exit fullscreen mode

Windows

  • Run buildconf.bat
  • configure.js --enable-olsrt
  • Build with MSVC → output php_olsrt.dll
  • Copy to ext\ and add to php.ini:
  extension=php_olsrt.dll
Enter fullscreen mode Exit fullscreen mode

🧪 test.php

<?php
if (!extension_loaded('olsrt')) {
    @dl('olsrt.' . (PHP_SHLIB_SUFFIX ?? 'so'));
}

olsrt_hello();
echo "Version: ", olsrt_version(), PHP_EOL;
Enter fullscreen mode Exit fullscreen mode

Run:

php test.php
Enter fullscreen mode Exit fullscreen mode

🎯 Conclusion

And that’s it! You’ve just built a PHP extension that binds to OLSRT. Right now it’s a skeleton with olsrt_hello and olsrt_version, but the structure is ready for you to plug in Futures, Actors, Event Loop, Streams, and WebSockets from OLSRT.

This is the foundation—next steps could be:

  • Wrapping OLSRT Futures into PHP classes
  • Adding async/await‑like APIs
  • Building a real demo (e.g. chat server)

👋 Final words

Thanks for following along! I hope this guide helps you bring the async/event‑driven magic of OLSRT into PHP.

If you try this out, let me know what you build—I’d love to see it.

Until next time, have a great OLSRT day! 🚀

Top comments (0)