Skip to content
master
Go to file
Code

Latest commit

Instead of doing an assert, do something with a more user-friendly
output for a somewhat common case where the user typed the filename incorrectly.

Imagine that you're doing this:

	$ kphp2cpp foobar.php

Where `foobar.php` doesn't exist.

kphp2cpp message before:
```
Fatal error [gen by $KPHP/compiler/compiler-settings.cpp at 293]
In stage = []:
  [file = unknown]
  [function = unknown function:-1]
  Assertion dir_pos != std::string::npos failed

Compilation failed.
It is probably happened due to incorrect or unsupported PHP input.
But it is still bug in compiler.
[pid 12570] [time 1605115955] SIGABRT caught, terminating program
[pid 12570] [time 1605115955] errno = No such file or directory
[pid 12570] [time 1605115955]
------- Register Values -------
[pid 12570] [time 1605115955] RIP=0x00007F063442DE97 RSP=0x00007FFF4A53EB60 RBP=0x00007FFF4A53EE70 RDI=0x0000000000000002 RSI=0x00007FFF4A53EB60 RDX=0x0000000000000000 RCX=0x00007F063442DE97 R8=0x0000000000000000 R9=0x00007FFF4A53EB60 R10=0x0000000000000008 RBX=0x0000000000000001 RAX=0x0000000000000000 CR2=0x0000000000000000 R11=0x0000000000000246 R12=0x00007FFF4A53FF80 R13=0x0000000000000003 R14=0x00007FFF4A53FC20 R15=0x0000000000000003
[pid 12570] [time 1605115955]
------- Stack Backtrace -------
./kphp(_Z15print_backtracev+0x53)[0xc5b6cc]
./kphp[0xc5bcec]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x12890)[0x7f0635131890]
/lib/x86_64-linux-gnu/libc.so.6(gsignal+0xc7)[0x7f063442de97]
/lib/x86_64-linux-gnu/libc.so.6(abort+0x141)[0x7f063442f801]
./kphp(_Z20on_compilation_errorPKcS0_iS0_12AssertLevelT+0x1bd)[0xbfbef5]
./kphp(_ZN16CompilerSettings4initEv+0xf78)[0xb95310]
./kphp(main+0x2392)[0x9110ba]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f0634410b97]
./kphp(_start+0x2a)[0x90dc2a]
[pid 12570] [time 1605115955] -------------------------------
[pid 12570] [time 1605115955] kphp2cpp compiled at Nov 11 2020 17:35:15 MSK by gcc 8.4.0 64-bit after commit a8ef101 branch null_coalesce_assign[pid 12570] [time 1605115955]
```

kphp2cpp message after:
```
Failed to open file [foobar.php] : No such file or directory
```

Signed-off-by: Iskander Sharipov <[email protected]>
7da7c6f

Git stats

Files

Permalink
Failed to load latest commit information.

README.md

KPHP — a PHP compiler

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.

Visit the KPHP website with documentation, demos, etc.

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.

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.

Benchmarks

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 by focusing on strict typing and KPHP built-in functions.

"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.

The License

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

Ask questions and provide feedback

To communicate with KPHP community, use Github issues or public E-Mails available on Github.

You can also take a look at our FAQ page.

Contributing

Please, refer to the Contributing page.

How do I start using KPHP?

Proceed to the installation page and just follow the left menu.

You can’t perform that action at this time.