#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    /root
    Posts
    14
    Rep Power
    0

    Thumbs down Pushing pointer to c++ class instance into object and getting it in other method


    Hello there.

    I am seeking some help with a small project. In particular, I am writing bindings to a C++ library (easywsclient). In PHP terms, it could be described as:

    PHP Code:
    <?php namespace easywsclient; class WebSocket {

        public 
    $url;
        public 
    $readyStates = array();
        public 
    $connection// Resource
        
    public $rxbuf;
        public 
    $txbuf;

        public function 
    __construct($url)
        public function 
    poll()
        public function 
    send($message)
        public function 
    dispatch(Closure $callback)
        public function 
    readyState()
        public function 
    close()

    ?>
    Doesnt sound hard, right?

    ..heh, in PHP logic, it doesnt, correct

    I have gotten so far, that a connection can be established. But now, I want to store the c++ class instance into $this->connection - as a resource. Resources, as it appears in the manual, are used to describe "complex data types, such as pointers"...huh, sounds OK. It seems that I can even create the pointer and store it - because I get soemthing like this:

    PHP Code:
    php $ws = new easywsclient("ws://echo.websocket.org");
    easywsclientconnectinghost=echo.websocket.org port=80 path=/
    Connected tows://echo.websocket.org
    php print_r($ws);
    easywsclient Object
    (
        [
    url] => ws://echo.websocket.org
        
    [ws] => 8589934592

    So appearently, "ws" DOES contain something - a unique id that probably represents my resource...thats fine, exactly what I want. But, lets say in the poll method, how do I access that pointer and turn it back into an instance of the actual c++ class? AFAIK, the stored pointer would point to the instance that I created - and i just need to extract the pointer from $this (probably using something like zval *_this = getThis() and turn it back into the instance, then do my low-level stuff with it.

    Another issue is yet, to pass a LAMBDA function (A closure object) to the dispatch method. My dispatch method was perfectly able to recieve the object, and returned it as well - i did that for testing. So, now I just need to convert it to a c++ "Callable"...how? I know that the colsure-object has a method named __invoke, which is a magic method to cause function execution once we treat the variable as a function, like so:

    PHP Code:
    <?php
    $anonFunc 
    = function($a$b) { echo pow($a$b); };
    $anonFunc(55); // 25
    ?>
    So far, so good.

    If there is anybody who possibly can somehow help me, I would be grateful. The object-related Zend API is so extremely poorly documented, that it is almost telling me to "not create an object at all" XD.

    Kind regards, Ingwie

    PS:
    Yes, I am a newcommer here, but surely none to PHP itself.
    Sorry for the wall, btw. XD

    EDIT
    I forgot to mention, the code is on GitHub. Since I can not post links: github.com -> IngwiePhoenix -> php-easywsclient
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    240
    Rep Power
    29
    I probably did not catch the idea, but it looks that passing code pointer via network is bad solution whatever you do.

    Even when you will try to reuse it, it may appear the object was removed (or moved?) and it is not easy to check this before using pointer in C.

    Perhaps it is worth keeping on serializing simple objects with JSON (which can be easily handled on php side). It is not as straightforward in C as in PHP, but I think you can decide how to implement it properly in your case.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    /root
    Posts
    14
    Rep Power
    0
    Well I somehow need to keep the class instance around - so I found this manual entry about resources which stated, that a Resource-Type is there to represent complex data in a PHP script environment - such as pointers. So to me it sounded like they were saying that I could store a pointer into a PHP object, tell the script "This is a resource", and then somehow grab the pointer back off the object once another of its methods is called.

    If i only knew how to grab an object's property from the C-side. I know, that I need to use the getThis() macro...but thats basically the only thing i know x.x

IMN logo majestic logo threadwatch logo seochat tools logo