What is the use of V8 in PHP?

PHP has a built-in JavaScript V8 engine, http://www.php.net/manual/en/book.v8js.php . I was wondering what are the possible use cases, especially. given that PHP is synchronous and JavaScript is asynchronous.

+4
source share
6 answers

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

A possible use case is to check the validation of the server and client using the same JS code. The client side, the code will be launched in the browser and on the server side, which it executed using V8JS.

Other potential applications could be templates or any other business logic that is needed both on the client side and on the server side.

It seems that you will still be in uncharted territory. I have not seen any libraries use V8JS for anything like that.

+4
source

One way to use JS integration in PHP is to provide an easy way for end users to safely execute user code (aka scripts) in a PHP-based application. With implicit isolation, you can limit the inputs and functions available to protect user privacy and system security, while allowing you to use a wide range of user scripts in the server context.

I have to admit that looking at javascript executable code in database records is sometimes a little annoying! You still get over it. :)

+3
source

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

You can write your own templates using the javascript framework, for example Vue.js, or edit it using v8js.

After php displays the template and sends it to the browser, the javascript framework can select it and make it interactive.

Two advantages here are faster web application loading and the absence of an annoying javascript page flex.

+1
source

PHP and Javascript are languages, it makes no sense to say that one is synchronous and the other is not. Informally, when they say that this means that most of the libraries around this language using I / O use async / sync IO.

In the main language and V8 nothing is asynchronous.

Why use it? Javascript on V8 is an order of magnitude faster than the canonical PHP implementation (note that there are other PHP implementations like Facebook HHVM ), although it is still the same as the powerful scripting language. Normally you will need to write a C extension for PHP to get raw performance.

0
source

For a construction crawler that extracts useful data from HTML by emulating Javascript runtime, it is especially useful because some HTML data may be hidden / corrupted by Javascript code. Therefore, if you do not want to write a Javascript parser in PHP yourself - the only option is to use the already built V8 engine for this purpose.

0
source

Sorry to pounce on an ancient question, but a pretty obvious use case in the Web 2.0 / 3.0 era would be "when file_get_contents () just won't cut it."

Although this may not be the best choice, PHP is also not the worst choice for bots, spiders, scraper, etc. And more and more web pages are not completed if JavaScript is not executed / displayed on them.

-one
source

Source: https://habr.com/ru/post/1495710/


All Articles