Build High‑Performance PHP Extensions with Go Using FrankenPHP

FrankenPHP lets developers write PHP extensions in Go, offering a generator and tool library that replace complex C code, simplify deployment, and leverage Go's concurrency, while being officially supported by the PHP Foundation.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Build High‑Performance PHP Extensions with Go Using FrankenPHP

Overview

PHP extensions are traditionally written in C, which requires deep knowledge of the PHP core and is a high‑barrier task. FrankenPHP introduces a generator that lets developers write PHP extensions in Go, leveraging Go’s lightweight concurrency model.

What is FrankenPHP?

FrankenPHP is a modern PHP application server built on Caddy and Go. It embeds a PHP interpreter, uses CGO for Go‑PHP interop, and provides a single‑binary deployment model.

Why write PHP extensions in Go?

Go offers a simple syntax, built‑in goroutine concurrency, and a rich standard library. Compared with C, Rust or C++, Go reduces the learning curve while still delivering high‑performance parallelism.

Core tools

1. Extension generator – zero‑C development

The generator scans Go source files for special //export_php directives and automatically creates the C glue code, header files, and PHP stub files.

package main

// export_php:function multiply(int $a, int $b): int
func multiply(a int64, b int64) int64 {
    return a * b
}

// export_php:function is_even(int $a): bool
func is_even(a int64) bool {
    return a%2 == 0
}

Run the generator: frankenphp extension-init ext-dir/ext.go The command creates a build directory containing: ext.c – generated C glue handling PHP‑Go interaction. ext.go – adjusted Go source compatible with the generated C. ext.h and ext_arginfo.h – header files defining function signatures. ext.stub.php – PHP stub file with function signatures. README.md – documentation of the generated extension.

The build directory is then supplied to Caddy, allowing the extension to run inside FrankenPHP without any manual C coding.

2. Tool library – simplified type conversion

FrankenPHP provides helper functions that hide Zend‑engine details: frankenphp.GoString() – converts a C string to a Go string. frankenphp.PHPString() – converts a Go string to a PHP string.

package main

// export_php:namespace Go\MyExtension
import (
    "C"
    "github.com/dunglas/frankenphp"
    "unsafe"
)

// export_php:const
const MY_GLOBAL_CONSTANT = "Hello, World!"

// export_php:class MySuperClass
type MyClass struct {
    Name string
}

// export_php:method MySuperClass::setName(string $name): void
func (mc *MyClass) SetName(v *C.zend_string) {
    mc.Name = frankenphp.GoString(unsafe.Pointer(v))
}

// export_php:method MySuperClass::getName(): string
func (mc *MyClass) GetName() unsafe.Pointer {
    return frankenphp.PHPString(mc.Name, false)
}

These helpers let developers focus on business logic instead of low‑level Zend internals.

3. Caddy module integration

Extensions are registered with the PHP interpreter via a simple init() function:

package ext

import "C"
import "github.com/dunglas/frankenphp"

func init() {
    frankenphp.RegisterExtension(unsafe.Pointer(&C.ext_module_entry))
}

This eliminates the need to write manual C registration code.

Use cases

High‑performance tasks – use goroutines for parallel image processing, data analysis, or asynchronous I/O.

Go library integration – wrap Go packages (e.g., etcd) as PHP extensions to extend PHP functionality.

Simplified deployment – bundle the generated build directory with FrankenPHP’s single‑binary server.

Learning and experimentation – generated C code serves as a reference for understanding PHP extension internals.

Getting started

Install FrankenPHP and Caddy according to the official documentation.

Create a Go file and annotate functions, classes, or constants with //export_php directives.

Run frankenphp extension-init to generate the extension files.

Provide the resulting build directory to Caddy and start the server.

Reference documentation at https://frankenphp.dev for detailed generator and library usage.

Considerations

Feature limits : the generator currently supports basic scalar types, classes, and methods; advanced object handling may still require hand‑written C code.

Performance trade‑offs : Go’s concurrency is fast, but crossing the Go‑PHP boundary introduces some overhead that should be evaluated per use case.

Community support : FrankenPHP is officially backed by the PHP Foundation, with an active GitHub community for issue tracking and contributions.

Conclusion

FrankenPHP’s extension generator and tool library enable PHP developers to write high‑performance extensions in Go, dramatically lowering the learning curve while preserving speed and flexibility. Backed by the PHP Foundation, Go‑based extensions are poised to become a first‑class part of the PHP ecosystem.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendGoPHPExtensionFrankenPHP
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.