sanket3dx/phpgo

True Go Concurrency for PHP - Native Extension

Maintainers

Package info

github.com/Sanket3dx/phpgo

Language:HTML

Type:php-ext

Ext name:ext-phpgo

pkg:composer/sanket3dx/phpgo

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 8

Open Issues: 0

v1.0.1 2026-01-09 11:00 UTC

This package is auto-updated.

Last update: 2026-02-09 11:09:11 UTC


README

phpgo is a native PHP extension written in Go that brings Go's concurrency primitives to PHP. It enables parallel execution using Goroutines, Channels, WaitGroups, and Select, backed by the Go runtime.

Features

  • Goroutines: phpgo\go(callable) spawns a lightness Go thread.
  • Channels: Buffered and unbuffered channels (phpgo\channel).
  • Select: Go-style select statement (phpgo\select).
  • WaitGroup: Synchronization primitive (phpgo\WaitGroup).

Requirements

  • PHP 8.0+
  • Go 1.18+
  • GCC / Clang
  • ZTS (Zend Thread Safety) PHP build is highly recommended for stability.

Architecture

phpgo operates by loading a Go-compiled shared object (libphpgo.so) into a thin C-based PHP extension (phpgo.so). The Go runtime manages the scheduling of goroutines and channel operations.

Installation

Option 1: Using Pre-compiled Binaries (Fastest)

  1. Download the phpgo.so from the build/ folder.
  2. Copy to Extensions: Move phpgo.so to your PHP extensions directory (e.g., /usr/lib/php/20210902/).
  3. Enable in PHP: Add extension=phpgo.so to your php.ini.
  4. Restart: Restart your web server (e.g., sudo systemctl restart php8.1-fpm).

Option 2: Build from Source

  1. Clone the repository.
  2. Build the Go library:
    go build -o go-build/libphpgo.a -buildmode=c-archive src/go/lib.go
  3. Build the PHP extension:
    phpize
    ./configure --enable-phpgo
    make
  4. Load it in PHP:
    php -d extension=`pwd`/modules/phpgo.so script.php

Usage

Channels & Goroutines

<?php
$ch = phpgo\channel();

phpgo\go(function() use ($ch) {
    echo "Sending from goroutine...\n";
    phpgo\send($ch, "Hello form Go!");
});

$msg = phpgo\receive($ch);
echo "Received: $msg\n";

Select

<?php
$ch1 = phpgo\channel();
$ch2 = phpgo\channel();

// ... spawn producers ...

$result = phpgo\select([
    phpgo\case_recv($ch1),
    phpgo\case_recv($ch2),
    phpgo\case_default(fn() => "Timeout")
]);

var_dump($result['value']);

Safety Warnings

  • NTS Builds: Running PHP code concurrently (inside phpgo\go) on Non-Thread-Safe PHP builds is experimental and may cause crashes if global state is accessed.
  • Resource Management: Channels are Go objects referenced by ID. They are garbage collected by Go when unreachable, but the ID mapping implies they should be closed explicitly or we rely on the map being cleared (TODO).

License

MIT