This is experimental
Run javascript inside PHP, powered by the awesome Duktape Javascript engine.
- It's fun!
- Javascript is becoming mainstream, with hundreds of libraries. Having an easy way of sharing code with Javascript natively makes using
phpize
./configure --enable-phpjs
make installThen add extension=phpjs.so to your php.ini
How to use it?
There is JS class, each instance runs it's own Javascript virtual machine (or duktape's context).
$js1 = newJS;
$js1->load("foobar.js");The JS object is like a proxy between the Javascript and the PHP userlands. In the PHP side, the JS looks like an Array or an object.
$js['foobar'] = 1; // Set `foobar` inside Javascript (global variable)var_dump($js['XXX']); // Read global variable 'XXX' from javascript$js->fnc(); // Call fnc() function from Javascript.Inside Javascript there are also some intergration. For instance we have a PHP (or $PHP) global object.
PHP.var_dump("something");// Call a PHP function!PHP.$something=1;// set a variableprint(PHP.$something_else);// read a variable from PHP@PARAMS $allowedVarAndFunc (Optional) : String[] Array to PHP function and global variables updatable via PHP's object
$VM = newJS(/*$allowedVarAndFunc*/);
// the VM can handle any PHP function and get/set any PHP global variables$VM = newJS(['$myVar','$args','$argv','apache_getenv','apache_get_module','headers','date']);
// the VM can handle only PHP function and get/set any PHP global variables listed@PARAMS $filename : String path to js source file (Absolute ou Relative)
$VM = newJS;
$VM->load("file.js"); // relative path, throw an error if file not found$VM->load("/tmp/file2.js"); // with an absolute path@PARAMS $jsCode : String path to js source to execute
$VM = newJS;
$VM->evaluate("var i = 1; print('From JS CODE',i);");
$VM->evaluate("i++;print('From JS CODE',i);"); // allow to share data across code evalution (it's a same context)PHPJS (Duktape) has a built-in minimal module loading framework based on CommonJS modules version 1.1.1, with additional support for module.exports.
Module is writen in javascript only
// Module foo/barexports.hello=function(){print("world");}You can load modules from javascript code with the global require() function:
varmod=require('foo/bar');mod.hello();The modules lookup function is the PHP's global JSModSearch or the local function $JS->JSModSearch
// Global lookup functionfunctionJSModSearch($id){
if ($id === 'foo') {
return'exports.hello = function() { print("Hello from foo!"); };';
} elseif ($id === 'bar') {
return'exports.hello = function() { print("Hello from bar!"); };';
}
}
/* shared PHP vars and function */$js = newJS([]);
$JS = 'var foo = require("foo");foo.hello();var bar = require("bar");bar.hello();try{ var nonExistentModule = require("nonExistentModule");}catch(e){ print("expected exception: " + e.message);}';
$js->evaluate($JS);/* shared PHP vars and function */$js = newJS();
// Local lookup function$js->JSModSearch = function ($id){
if ($id === 'foo/bar') {
return'exports.hello = function() { print("Hello from foo/bar!"); };';
} elseif ($id === 'bar') {
return'exports.hello = function() { print("Hello from bar!"); };';
}
};
$JS = 'var foo = require("foo");foo.hello();';
$js->evaluate($JS);For more documentation look tests folder
- Better sharing of objects between PHP and Javascript
- More documentation
- More integration :-)