Go library plug-and-play to parse PHP code through the embed SAPI + ext-ast, returning the AST as JSON.
No PHP installation required: prebuilt binaries are automatically fetched on first use.
go get github.com/Halleck45/go-php-parserGo 1.20+ recommended.
CGO_ENABLED=1must be active (default on macOS/Linux).
- Linux:
linux_amd64 - macOS:
darwin_amd64,darwin_arm64
package main
import (
"fmt"
"github.com/Halleck45/go-php-parser"
)
func main() {
defer PhpParser.Shutdown()
// Inline code
json, ok := PhpParser.ParseCode("<?php function foo(int $a){return $a+1;}", "inline.php", 0, 0)
if !ok {
panic("parse failed")
}
fmt.Println(json)
// File
json2, ok := PhpParser.ParseFile("example.php", 0, 0)
if !ok {
panic("parse file failed")
}
fmt.Println(json2)
}The parser processes 4,000–8,000 PHP files per second on a standard development machine (multi-core CPU, NVMe SSD).
For advanced usage, you can pass the following parameters:
astVersion:0⇒ current version ofext-ast(recommended).flags:0(default).
The lib returns JSON with at least:
root: AST array (viaast\dump(..., AST_DUMP_ARRAY))version: metadata (PHP, ext-ast, ast_version, engine)file(if provided)
MIT (see LICENSE).
