Home » Php » What are use cases for V8 in PHP?

What are use cases for V8 in PHP?

Posted by: admin July 12, 2020 Leave a comment

Questions:

PHP has embedded V8 JavaScript engine, http://www.php.net/manual/en/book.v8js.php. I was wondering what are possible use cases, esp. considering that PHP is synchronous and JavaScript is asynchronous.

How to&Answers:

A powerful feature of this extension is that the same code can be used both client and server side, reusing implementations of the same code in PHP for server side and js client side.

A possible use case is for providing server and client side validation using the same JS code. Client side, the code would be run in the browser, and server side it’s executed using V8JS.

Other potential uses could be templates or any other business logic that’s needed both client and server side.

It seems like you’ll still be in uncharted territory here, though. I haven’t seen any libraries use V8JS for anything like this.

Answer:

One of the ways I am using JS integration within PHP is to provide a simple manner for end users to safely execute user-supplied code (a.k.a. scripts) within a PHP-based app. With the implicit isolation, one can limit the inputs and functions available to protect both user privacy and system security while allowing a wide range of user-defined scripted actions within the server context.

I must admit that seeing executable javascript code in database records is a bit unnerving at times! You do get over it though. 🙂

Answer:

One use case could be server-side rendering for javascript templates or frameworks, SSR for short.

One could write their templates with a javascript framework like Vue.js or react and render it with v8js.

Once php has rendered the template and sent it to the browser the javascript framework could pick this up and make it interactive.

Two benefits here are faster loading web apps and no annoying javascript page flicker.

Answer:

For building crawler which extracts useful data from HTML emulating Javascript runtime is especially useful, because some HTML data may be hidden/mangled by Javascript code. So unless you would like to write Javascript parser in PHP yourself – the only option is to use V8 engine built already with that purpose.

Answer:

PHP and Javascript are languages, it doesn’t make sense to say one is synchronous and one is not. Informally when one says that, they mean that most of the libraries around that language that use I/O use async/sync IO.

In the core language and V8, nothing is asynchronous.

Why to use it? Javascript on V8 is orders of magnitude faster than the canonical PHP implementation (note that there are other PHP implementations, like Facebook’s HHVM) while still being just as powerful scripting language. Normally you would be forced to write a C extension for PHP to get raw performance.

Answer:

Sorry to bump / add to an ancient question, but a fairly obvious use case in the Web 2.0 / 3.0 era is going to be “when file_get_contents() just won’t cut it.”

Although it may not be the best choice, PHP also isn’t the worst choice for bots, spiders, scrapers, etc. And more and more web pages aren’t complete unless the JavaScript on them is executed / rendered.