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.