定制 sanket3dx/phpgo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sanket3dx/phpgo

最新稳定版本:v1.0.1

Composer 安装命令:

pie install sanket3dx/phpgo

包简介

True Go Concurrency for PHP - Native Extension

README 文档

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

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 0
  • Forks: 0
  • 开发语言: HTML

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-09