Blog Detail

23

Sep
KPHP - A Robust PHP Compiler for Better Code Optimization cover image

arrow_back KPHP - A Robust PHP Compiler for Better Code Optimization

KPHP is a PHP compiler. It compiles a limited subset of PHP to a native binary running faster than PHP. KPHP was developed at VK.com and maintained as proprietary for years — until open-sourced in late 2020.

Limitations

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 VK.com 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.

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.

KPHP vs PHP differences

KPHP is a compiler, PHP is an interpreter

First of all, KPHP is a compiler. It analyzes your whole project and compiles it to a single Linux binary.

More detailed, KPHP translates PHP sources to C++ equivalent and then compiles/links the resulting C++ code. Limitations of compilability take their roots from C++ — they are similar to all compiled languages.

In PHP, if you made an error in your code — you’ll see it only when the execution point reaches that line.

In KPHP, you are unable to build your site until you fix all errors.

In PHP, symbols are resolved while executing. If you forget require_once, PHP will fail. You can define() in the middle of the script. Classes are dynamically autoloader.

In KPHP, all symbols are resolved at compile-time. You can call any existing function from anywhere — it just exists. All constants are inlined, they don’t appear dynamically. A full list of classes is known in advance.

KPHP doesn’t support features, that break

type system
f(42);
f("string");
f(new User);
array(1, new User, function(){});

In PHP, you can mix types arbitrary and handle them at runtime. You pass numbers/arrays/objects to the same function. You create hashmaps of anything — and it just works if you access it by correct indexes.

In KPHP, you should always think about types — like in any other compiled language. If you mix types incorrectly, it’s a compilation error. You’ll need to rewrite lots of your PHP code to satisfy the type of system.

KPHP doesn’t support features, that can’t be compiled

$func_name = "action_handler_" . ($input['action'] ?: 'default');
call_user_func($func_name, $_GET);

You can’t call a function by name in any compiled programming language. But you can do this in PHP — and in PHP it’s a very common scenario for routing/factories/etc. It won’t work in KPHP.
As a consequence, you can’t call methods by name, you can’t access variables by name, and so on.

KPHP has a lot more features and options, If you wanna know the installation & setup details, You can visit it’s complete documentation here.

Note

If you are a business and want to leverage Laravel for your next PHP-based web application, CRM Software, E-commerce application, SAAS application, or any custom web application development for your custom requirements, you must discover an amazing team with experience in the Laravel framework. So Codebrisk is here to help you with your tailored requirements regarding Laravel Development. Please feel free to send us an email at rizwan@codebrisk.com or get in touch with us, our business person will get back to you.

Published at : 23-09-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project