Documentation

php2go documentation

A practical user guide: prepare a ZIP, choose an entrypoint, run allowed commands, download the artifact, and understand common compiler errors.

Fast path to the first build
  1. Pack the project into a ZIP without secrets or junk files.
  2. Make sure the entrypoint exists, usually public/index.php.
  3. Run report/check/deps before compile.
  4. Download the artifact and run the binary using README_RUN.
GO OUTPUT

How to write PHP for faster Go

The compiler can optimize a lot of old PHP, but the best result comes from code with clear types, stable classes, and predictable arrays.

Helps the compiler

  • Add argument and return types where they are known.
  • Pass arrays and DTOs explicitly instead of hiding structure in globals.
  • Keep entrypoints, routing, and services in clear classes with Composer autoload.
  • Use simple conditions and explicit fallbacks instead of deep magic and arbitrary callbacks.

Often causes fallback

  • Dynamic class names without a recognizable namespace prefix.
  • Variables that become an array, string, object, and number in different branches.
  • Magic properties and methods without stable initialization.
  • Large vendor/test/dev directories that are not used at runtime but enter analysis.

Analysis-friendly style

PHP source
function userId(array $user): int
{
    return (int) ($user['id'] ?? 0);
}
Typical Go output
func f_userId(v_user php.Array) int64 {
    return php.ArrayGetIntDefault(v_user, php.StrKey("id"), 0)
}
Why this is faster

The compiler sees array → int and can emit a direct typed accessor instead of a generic PHP conversion at runtime.