phpv8/php-v8

Calling php methods from js

danieleratti opened this issue · 4 comments

It's not clear to me if is possibile like in v8js to call php methods, inject input data and classes inside the js code or get variables outside to interact more.
Is there any documentation?

Hi @danieleratti!

It's possible to call any PHP function/method from js runtime, pass data in a both way and so on. Actually, in php-v8 you can do everything V8 provides. Unfortunately, there is no documentation yet (here is a ticket for that - #48). However, you may look into tests folder and see some examples.

I'd like to note php-v8 key difference from v8js: php-v8 is a low-level binding to V8 API, while v8js provides high-level abstraction. I actually like v8js and it's a great tool, however for my need I had to make php-v8 as it is. To deal with php-v8 and v8 complexity I have a PHP library which I plan to open source soon. With raw php-v8 you will operate on a V8 C++ API level, so here comes php-v8 pros and cons: you can do anything, but you have to know how.

In short, from js perspective there is no big difference between function and method - everything is a function and you can add/remove object "methods" in js at runtime, so here comes a difference between PHP and JS and how it glues together: you wrap any PHP callback with with a new FunctionObject(Context $context, callable $callable) (or you can use FunctionTempalte for that, but it's a bit advanced level).

To compose JS object form PHP you would probably build new ObjectValue(Context $context) and set it properties with ObjectValue::set(Context $context, StingValue $name, Value $value). It might be a bit verbose, but it's handled in my php library.

Note, that php-v8 is under heavy development so at the moment it's API could not be considered as final and it might be a subject of further changes. However, in a pending v0.2.0 release which I schedule to the end of September (actually, I plan to do that during this weekend) the API is stabilized. In advance PHP library that provides high-level abstraction to php-v8 will take care of any BC-breaking changes in php-v8 and will have much more stable API.

Feels free to write more if you have any question or you need assistance to dive into php-v8 or solve particular case.

@pinepain great! Thank you very much for your clarifications. I will try the FunctionObjects and wait for the v0.2.0 release :-)

Hi! Short status update: v0.2.0 release tagged.

In advance, I published wrapper library for php-v8 - https://github.com/pinepain/php-v8-js-sandbox/tree/wip-public-release. The work is still in progress, so feels free to suggest changes and ask questions. It was made for my side projects needs and you may find ad-hoc solutions there. If you will be lucky to do that, I would appreciate if you report that to me. Also, feels free to ask questions: the doc and tests are not the strongest part and they are also in WIP state.

If you like what I'm doing and you use it in you work, I'd love if you support me on Patreon. I have plenty ideas and improvements in mind, but doing all that over evenings and weekends is not sustainable in the long term.

UPD: First version of https://github.com/pinepain/js-sandbox released. As it is under heavy development, use at your own risk.

Closing this issue for now as not much to do about it. Some docs available at https://pinepain.gitbooks.io/php-v8/content/. It's a bit limited for now, though, more to come. For more docs feels free to look into stubs and tests. As always, I'm glad to answer any question regards php-v8 and it's usage. PRs to code, stubs and docs are more then welcomed.