PHP: v8.js
php.net
php.net
I wouldn't object so much if only the PHP community weren't already known for their tendency to take a bad situation and make it worse: can't we learn from the history of mysql_query() and friends? Decades of SQL injections! There's no reason they couldn't provide a nice "JsObject" type supporting __call() and __get() to allow integration without resorting to interpolating bits of JavaScript code with (probably originating from a user) data, this way at least web developers would be safer from themselves.
Therefore we started two weeks ago to evaluate javascript rendering of mustache templates from inside PHP. First we tried the spidermonkey extension, but had some issues with it. Currently we are using v8js on mac and ubuntu. Works like a charm so far, and we have to write template functions only once.
Now we can use the same mustache templates AND helper functions from within PHP and client side javascript. Even compiled hogan [2] templates work. The templates and javascript files are stored in the PHP APC cache to avoid disk seeks.
So far we are very happy with the results. Here is some php pseudo code how it works with mustache:
pseudo-php-example: \V8Js::registerExtension("mustache", apc_fetch("hogan.js-content")); $v8 = new \V8Js('ContextName', array(), $extensions); $v8->view = array("stuff" => "available in the js template"); $v8->view = array("partialName" => apc_fetch("partialTemplateContent")) $v8->template = "{{stuff}}"
$script .= 'Mustache.render(PHP.template, PHP.view, PHP.partials);'; $renderedTemplate = $v8>executeString($script); echo $renderedTemplate; // prints available in the js template
I recommend checking the v8js samples [3].
[1] http://mustache.github.com/ [2] http://twitter.github.com/hogan.js/ [3] http://svn.php.net/viewvc/pecl/v8js/trunk/samples/
Probably easier to just php `exec()` a node script that does what you want. In fact, if you're considering using this, you'd be better off using node since it will almost undoubtedly perform better.
I experimented with (py) V8 as an engine for executing certain scripts from Python, but found there was a relatively cost to entering the interpreter. I was also missing a way to pass around buffers and not converting everything to unicode strings (maybe if I could wrap the ByteArray type).
For simple examples it worked fine, but trying it on real projects such as coffeescript it kept seg faulting.
It's definitely got potential though..
It could definitely be handy for a lot of reasons. Seems like it might be handy for embedding a bit of JavaScript testing in your PHP test suite.
I can't think what the result would be if the last variable instantiated in the JavaScript code was a function though; perhaps null, or an exception would be thrown.
So I guess my question is, why? What's a real use case example of why'd I want this? PHP isn't syntactically more tragic than JS. JS doesn't have so many more libraries than PHP already has? I can't see why someone would use this? Maybe to hook into GWT librarieS?
In my experimentation I wanted to see [in php after js execution] all the variables created in the global scope. There are two ways to do it, pass the flag to the V8js constructor to make executeString return associative arrays and add "this;" to the tail of the scripts you execute. Or, append "JSON.stringify(this);" to the tail of the scripts.
The first method will return everything including references to print, var_dump, sleep and exit methods that are bridged from PHP.
The stringify method will give you just the variables without the method references but tends to leave you with empty arrays if all your array indices point to null values. Which is misleading if the result is "someVal: {}", verses "someVal: {'foo': null, 'bar': null}".
What it misses most, is the ability to bridge your own method calls. Would be nice to be able to register a method call in JS that executes a user defined PHP function.
I'm afraid you can not get much out of the great features of JS. Event drivenness..
Anyway, this blows my mind!
Anecdotally - let's face it; there is no shortage of crappy php and javascript out there. This is an open invitation to merge the two into one extraordinary megaclusterfark.
<script src="/client-side.js"></script>
<noscript><img src="/server-side.js"></noscript>
...which fulfills the requirement of "executing some JS code even if JS is disabled in browser".I genuinely can't think of a way this will be used which will be positive for PHP.
In general I'd be cautious about both the ruby racer and this as you are practically embedding a garbage-collected heap into another garbage collected heap, exchanging references between them (more so for the ruby racer - the PHP extension here is way less integrated).
This is memory errors waiting to happen.
That sounds great until you realize that you usually do a lot more validation on the server than on the client (is the username taken, is password correct, etc etc). So you would still have to use PHP for validation (in most cases).
I think sharing code is always better than not sharing code. If you run into a use case where this would be useful, then you can just grab it and install it.
If you've been doing a lot of work in PHP recently, then you should be compiling your own PHP. Which gives you the option of disabling every extension. The PHP core is actually very lean. Virtually everything is an extension. They even have a nifty flag to disable everything.
I like having the option of compiling a bazillion extensions in the binary if I want to. Even though I never enable anything beyond the basic stuff I need.
PHP - forgot something? Best re-compile so I can access the module's massive list of global functions.
Python, Ruby, Node, etc. - `import/include/require module`
* because Pear is basically a waste of time.
Have you tried to install something using pip for Python? How about npm for Node?
Those package management programs compile stuff on your system all the time. Same as PECL extensions.
The ecosystem for Python, Ruby, Node works exactly the same as it does for PHP. Please leave your random PHP hatred at the door.