phpv8 / php-v8

PHP extension for V8 JavaScript engine
https://php-v8.readthedocs.io
MIT License
217 stars 14 forks source link

V8Js::executeString #92

Closed huoding closed 6 years ago

huoding commented 6 years ago

Do php-v8 has a way of doing something like V8Js::executeString, please show me a example, thanks.

pinepain commented 6 years ago

Hi, thanks for the question! php-v8 is more flexible though more verbose on doing such things. I guess what you asking about is covered in readme under "Hello, World!" example:

<?php
$isolate = new \V8\Isolate();
$context = new \V8\Context($isolate);
$source = new \V8\StringValue($isolate, "'Hello' + ', World!'");

$script = new \V8\Script($context, $source);

// You may wrap it in try-catch block
$result = $script->run($context);
// Btw, result may be non-scalar so you'd better check it type and act accordingly, `::value()` method is avalable on scalars only (non-objects)
echo $result->value(), PHP_EOL;

More complex examples are following the same approach.

While there is some docs at https://pinepain.gitbooks.io/php-v8/content/ , they are largely under construction or missed at all, so I'd suggest you to look into test folder or ask questions here.

If you are up to try something new, feels free to give https://github.com/pinepain/js-sandbox a look, it supposed to be a high-level abstraction on top of php-v8. Note that it's under construction and I largely focuses there on embedding into Laravel application using their DI (see src/Laravel folder). If you looking for embedding js into your application, I would recommend to give js-sandbox a look too.

pinepain commented 6 years ago

@Fatih90 do you have a minute to sound out what approach you'd see as a more advanced solution in this ext, taking into account that this is a low-level wrapper around v8 itself? I would really appreciate it as it would give me much more feedback than just 👎 .

pinepain commented 6 years ago

I was looking for some feedback, but it's not bad too, thanks for your time!

huoding commented 6 years ago

@pinepain thanks