PHP code example of psx / v8

1. Go to this page and download the library: Download psx/v8 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

psx / v8 example snippets




$script = <<<JS

var message = 'Hello ' + console.foo;

resp = {
    message: console.log(message),
    bar: function(data){
        return 'Foo ' + data;
    },
    foo: 'bar'
};

JS;

$environment = new \PSX\V8\Environment();

$environment->set('console', [
    'foo' => 'foo',
    'log' => function($value){
         return $value . ' and bar';
    }
]);

$environment->run($script);

$resp = $environment->get('resp');

echo $resp->get('message') . "\n"; // Hello foo and bar
echo $resp->get('bar')(['test']) . "\n"; // Foo test
echo $resp->get('foo') . "\n"; // bar




class Console
{
    public $foo;
    
    public function __construct()
    {
        $this->foo = 'foo';
    }
    
    public function log($value)
    {
        return $value . ' and bar';
    }
}

$console = new Console();

$environment = new \PSX\V8\Environment();
$environment->set('console', new \PSX\V8\Object\ReflectionObject($console));
$environment->run($script);

$resp = $environment->get('resp');