What are use cases for V8 in PHP?
Asked Answered
T

6

7

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.

Tirrell answered 7/8, 2013 at 9:55 Comment(4)
this link may help you to understand your question developerknowhow.com/why-the-v8-javascript-engine-is-so-goodZincography
Not sure what you mean by saying JavaScript is asynchronous - it's not. The only use case I can think of is to offload some complex math calculation, since V8 generates/JITs machine code and will run the code faster than native PHP.Chimera
@Chimera I imagine OP was thinking of Node.js. Although node is sequential almost all of the APIs are async (http, filesystem, etc.).Oscular
An old question, but I found it as top result in Google; I've recently started using it due wanting to provide clients a pre-rendered page. A simple example, imagine you are using a javascript library as Moment for showing relative times and you let the client update those times every minute afterwards. It wouldn't be nice if the formats are then different after a minute.. Allowing me to work with it the same way server-side allows me to keep things simple and be sure that it will spit out the same pre-rendered output client-side. Given the amount of javascript libraries nowadays, a time saverJylland
S
7

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.

Sunn answered 5/3, 2014 at 17:31 Comment(2)
I have since then built a validation library that used V8JS. However, it did not leave the prototype garage. After implementing it in a few projects I realised that PHP developer's do not necessarily want to mix backend and frontend logic, even if it is re-used across both. The code does not mix together well. Besides, usually backend and frontend development is carried out by separate teams and frontend developers might have their own preference for validation libraries. It is simpler for backend validation to extract rule names than the whole validation logic.Tirrell
I've been thinking about how a validation system would work using V8JS, and wondering why there hasn't been a semi-popular one around yet. My guess was that it just doesn't work out very well. There are a few projects that use V8JS though: A React.js renderer: github.com/reactjs/react-php-v8js A Handlebars renderer: github.com/SinisterMinister/php-barly and a BEM renderer: github.com/zxqfox/php-bemSunn
C
5

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. :)

Calida answered 27/7, 2014 at 21:55 Comment(0)
K
2

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.

Katmandu answered 22/7, 2016 at 2:6 Comment(1)
Question, can SSR still make ajax requests when javascript is disabled in the browser?Nth
I
1

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.

Inunction answered 8/9, 2017 at 12:4 Comment(0)
B
0

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.

Bonny answered 14/8, 2013 at 9:53 Comment(0)
A
-1

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.

Acuity answered 19/5, 2015 at 19:45 Comment(1)
What are you suggesting? Fetching a web resource using V8 would not make it execute the actual script. It is possible, I suppose. Nevertheless, a better alternative would be a heatless browser, such as phantomjs.Tirrell

© 2022 - 2024 — McMap. All rights reserved.