Coder Social home page Coder Social logo

Calling php methods from js about php-v8 HOT 4 CLOSED

phpv8 avatar phpv8 commented on August 23, 2024
Calling php methods from js

from php-v8.

Comments (4)

pinepain avatar pinepain commented on August 23, 2024 1

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.

from php-v8.

danieleratti avatar danieleratti commented on August 23, 2024

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

from php-v8.

pinepain avatar pinepain commented on August 23, 2024

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.

from php-v8.

pinepain avatar pinepain commented on August 23, 2024

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.

from php-v8.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.