Without polyfills, invoking such functions would compile and work on KPHP, but fail on PHP.
Read more about polyfills here.
Polyfills are in a Composer package, making them very easy to install.
Polyfills will be available on packagist soon, making the installation a one-line command; for now, use this:
composer.json
with these contents: {
"repositories": [
{
"type": "vcs",
"url": "git@github.com:VKCOM/kphp-polyfills.git"
}
],
"require": {
"vkcom/kphp-polyfills": "^1.0.0"
}
}
composer install
require_once '.../vendor/autoload.php';
And that's it, you're done! All KPHP functions now work on plain PHP.
You might already have a PHP development environment.
If your code is executed remotely, you might typically sync your changes with SFTP, or use sshfs to make local changes instantly appear on the server. Most likely, you already have nginx or Apache server in front of PHP.
For local purposes, you can just use an embedded PHP server: php -S
If you don't, you can easily start locally with the same Docker you already have installed for KPHP.
Create a project in ~/kphp-project/
with index.php
main file. Run this to map your folder to /tmp/dev
:
# if you have installed KPHP via the Docker registry
docker run -ti -v ~/kphp-project/:/tmp/dev:rw -p 8080:8080 vkcom/kphp
# or, if you have build an image from the Dockerfile locally
docker run -ti -v ~/kphp-project/:/tmp/dev:rw -p 8080:8080 kphp
Then navigate to /tmp/dev
, a mirror of your local folder.
# (inside Docker)
cd /tmp/dev
php -S 0.0.0.0:8080
Use PHP for development, but keep in mind that the compilation will fail if you write incorrect code.
For instance, PHPUnit can never be compiled by KPHP. You don't have to compile it, though, since testing is part of development and shouldn't be compiled for production. Read more here.
When you are certain that the PHP code works, it's time to compile it.
The compilation is invoked by a single command: kphp [options] index.php
KPHP has many limitations and guidelines. Without experience, the compilation would probably fail even for small pieces of code. You would have to correct all the errors for it to run. Read about KPHP vs PHP differences.
After compilation, you'll get a ./kphp_out/server
binary. We've already seen this when compiling a sample script.
Stop the PHP server and launch the KPHP binary on the same port.
./kphp_out/server -H 8080 -f 1
You can pass various compiler options to the kphp
command or execution options to the ./server
binary.
There is a kphp-inspector tool — a handy thing for querying and examining codegenerated C++ sources.
For simple scripts and playground projects, this might seem excessive to implement.
Read about maintaining, just follow the left menu.
Currently, KPHP can't connect to standard databases. While using KPHP as a proprietary tool, we never needed to implement support for that. VK has custom engines and data storage, which aren't made open source yet.
In other words, for now, KPHP may be interesting to experiment with. When all real-world necessities of such a tool are implemented, you would be able to try it for real projects.
This is explained in detail on the next page.