KPHP — a PHP compiler

GitHub page

KPHP is a PHP compiler. It compiles a limited subset of PHP to a native binary running faster than PHP.

KPHP takes your PHP source code and converts it to a C++ equivalent, then compiles the generated C++ code and runs it within an embedded HTTP server. You could call KPHP a transpiler, but we call it a compiler.

KPHP is not JIT-oriented: all types are inferred at compile time. It has no “slow startup” phase.

KPHP was developed at and maintained as proprietary for years — until open-sourced in late 2020.


KPHP wouldn't compile just any random PHP code:

  • It doesn't support features that can't be compiled, such as calling by name or mocks.
  • It won't compile code, that breaks the type system, for instance, mixing numbers and objects in an array.
  • It doesn't have PHP features that never had a need for, such as SPL classes and XML parsing.
  • Some PHP syntax details just weren't implemented, like generators and anonymous classes.

You can read more on this here: KPHP vs PHP differences.

Features over PHP

KPHP analyzes your code as a whole and performs various optimizations focusing on performance and safety:

  • Inferring types of all variables, how to declare them in C++.
  • Compile-time optimizations, such as inlining getters or reducing refcounters flapping.
  • Compile-time checks, including immutability and type system requirements.
  • Runtime optimizations, like constant arrays pre-initing and typed vectors.

Aside from the above, KPHP has coroutines. For now, however, they are almost inapplicable outside of VK code.


Generally, when your code fits best practices, it runs 3–10 times faster than PHP.

Take a look at the benchmarks page comparing KPHP and PHP performance.
You can also refer to PHP vs KPHP vs C++.

KPHP isn't always faster than PHP, but it can be used to speed up your code:

Another essential KPHP aspect is “zero-cost abstractions”. Create an unlimited number of defines and constants — they don't exist at runtime. Simple functions like getters are inlined and have no overhead at all.

KPHP performance compared to PHP:

  • “a random piece of PHP code can work faster” — if you’re lucky, then yes, but it might not.
  • “but I can optimize it to run faster” — usually yes, focus on clean types and KPHP built-ins.
  • “KPHP is faster in large projects with tons of abstractions” — usually yes.

“I tried to compile PHP code, but failed”

This situation is quite common. KPHP rarely compiles already existing code without errors. It usually takes some time to rewrite PHP code for it to be compilable with KPHP.

Read more about this in compiling an existing project.

Typical development with KPHP

  • Use PHP for development: sync your IDE with the dev server and write code. It's uploaded automatically and just works — PHP is an interpreter.
  • Use PHP for testing and bundling: PHPUnit can't and shouldn't be compiled.
  • Compile a server with KPHP and deploy it to production.
  • Monitor logs and statistics on production.

You can organize your project in a way that one part of it would be compiled, and the other would work on PHP.

The License

KPHP is distributed under the GPLv3 license, on behalf of (V Kontakte LLC).

Ask questions and provide feedback

To communicate with KPHP community, use GitHub issues or a Telegram chat.

You can also take a look at our FAQ page and Roadmap page.


If you've developed a useful PHP library that successfully compiles with KPHP, feel free to share it here.

If you want to contribute to KPHP itself, try to dig into its internals. They're quite hard to take in all at once. Learn about KPHP architecture and examine imaginary contribution scenarios.

How do I start using KPHP?

Just follow the left menu. KPHP's documentation is organized in a natural learning order.