This extension requires PHP >= 7.1. Last version that supports PHP 7.0 is v0.1.9.
This extension is still under heavy development and its public API may change without any warning. Use at your own risk.
- accurate native V8 C++ API implementation available from PHP;
- solid experience between native V8 C++ API and V8 API in PHP;
- no magic; no assumptions;
- does what it is asked to do;
- hides complexity with isolates and contexts scope management under the hood;
- provides a both-way interaction with PHP and V8 objects, arrays and functions;
- execution time and memory limits;
- multiple isolates and contexts at the same time;
- it works;
With this extension almost everything that the native V8 C++ API provides can be used. It provides a way to pass PHP scalars, objects and functions to the V8 runtime and specify interactions with passed values (objects and functions only, as scalars become js scalars too). While specific functionality will be done in PHP userland rather than in this C/C++ extension, it lets you get into V8 hacking faster, reduces time costs and gives you a more maintainable solution. And it doesn't make any assumptions for you, so you stay in control, it does exactly what you ask it to do.
With php-v8 you can even implement nodejs in PHP. Not sure whether anyone should/will do this anyway, but it's doable.
<?php $isolate = new \V8\Isolate(); $context = new \V8\Context($isolate); $source = new \V8\StringValue($isolate, "'Hello' + ', World!'"); $script = new \V8\Script($context, $source); $result = $script->run($context); echo $result->value(), PHP_EOL;
which will output
Hello, World!. See how it's shorter and more readable than that C++ version? And it also doesn't limit you from V8 API utilizing to implement more amazing stuff.
If you are also using Composer, it is recommended to add the php-v8-stub package as a dev-mode requirement. It provides skeleton definitions and annotations to enable support for auto-completion in your IDE and other code-analysis tools.
composer require --dev pinepain/php-v8-stubs
This extension is PHP7-only. It works and tested with both PHP 7.0 and PHP 7.1.
This extension works and tested on x64 Linux and macOS. As of written it is Ubuntu 16.04 LTS Xenial Xerus, amd64 and macOS 10.12.5. Windows is not supported at this time.
$ sudo add-apt-repository -y ppa:ondrej/php $ sudo add-apt-repository -y ppa:pinepain/php $ sudo apt-get update -y $ sudo apt-get install -y php7.1 php-v8 $ php --ri v8
OS X (homebrew)
$ brew tap homebrew/dupes $ brew tap homebrew/php $ brew tap pinepain/devtools $ brew install php71 php71-v8 $ php --ri v8
For macOS php-v8 formulae and dependencies provided by pinepain/devtools tap.
Building php-v8 from sources
git clone https://github.com/pinepain/php-v8.git cd php-v8 phpize && ./configure && make make test
To install extension globally run
$ sudo make install
to be able to customize some tests make sure you have
variables_order = "EGPCS"in your php.ini
export DEV_TESTS=1allows to run tests that are made for development reasons (e.g. test some weird behavior or for debugging)
To prevent the test suite from asking you to send results to the PHP QA team do
To run tests with memory leaaks check, install
To track memory usage you may want to use
lsofto see what shared object are loaded and
freeto display free and used memory in the system.
pinepain/libv8-experimental normally contains
libv8version that used in current
My thanks to the following people and projects, without whom this extension wouldn't be what it is today. (Please let me know if I've mistakenly omitted anyone.)
- v8js PHP extension which used as a reference on early stages;
- Stefan Siegl, for his profound work on v8js PHP extension and for his personal time at helping building V8;
- all v8js contributors;
- Jérémy Lal, one of libv8 maintainers for his personal help on building V8 on Ubuntu;
- John Gardner for dealing with V8 building system changes;
- @ilovezfs for his help and mentoring on upgrading V8 homebrew formulae.
Copyright (c) 2015-2017 Bogdan Padalko <[email protected]>