On Tue, 2013-10-01 at 15:01 +0200, Kevin Ingwersen wrote:
> Hello, and thank you for your response.
>
> I must have totally forgotten to append the github repository, where I
> host the current state of the development.
> https://github.com/IngwiePhoenix/php-easywsclient
> In php_easywsclient.cpp, look for this line:
>
> PHP_METHOD(easywsclient, readyState) {
>
> That is where I am currently working on getting the property $this->ws
> which I have previously stored via __construct. But I have the slight
> feeling I didn't do it right in general.
>
> What I am trying to do:
> 1. __construct creates a new class instance to the c++ class. Looks
> like so: easywsclient::WebSocket::pointer ws =
> easywsclient::WebSocket::from_url((std::string)url)
> 2. Store a pointer to ws as a resource into $this->ws.
> 3. In another method, extract $this->ws and form it back into the
> class instance.
>
> From what I know, pointers are integers. so I probably will have to do
> something like this:
No! Pointers are pointers.
Pointers contain a numeric address and can be printed somewhat like
integers but the aren't (for a start sizeof(void*) > sizeof(int) on some
platforms, so a integer can't contain all possible values, also
operators behave differently. operator+(1) on a pointer doesn't
increment the address by 1, like an integer does but by the size of the
pointed object)
> int ws_ptr = (function to get $this->ws)
> easywsclient::WebSocket::pointer ws =
> (easywsclient::WebSocket::pointer)ws_ptr;
Don't cast a pointer to an integer type unless you really understand
what you are doing and know what your CPU and compiler are doing. And
then still don't do this
> I am very new to C/C++ development, and mainly doing it because I
> really need the functions from the c++ class in PHP scripts.
>
> When I define a struct like you demonstrated with my_object, how do I
> then use it?
I see you defined a "new" handler (easywsclient_create_handler) but are
never calling it. See slide 86 from the deck referenced below for
registration in MINIT (you can ignore the iterator stuff there, marking
final makes things easy, else there is no guarantee that __construct is
being called etc, clone you can ignore for a start, but you should think
about it, best might be to disallow cloning)
Once your handler is being used to create the object you can access your
easywsclient_object as shown on slide 92.
Something like this
PHP_METHOD(easywsclient, __construct) {
char *url;
int url_len;
zval *res;
zval *_this = getThis();
easywsclient_object obj =
(easywsclient_object*)zend_object_store_get_object(_this TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &url, &url_len)
== FAILURE) {
/* you might want to throw an exception here or switch to exception
error mode
using zend_replace_error_handling() else the user ends up with an
unitialized object */
return;
}
obj->ws = easywsclient::WebSocket::from_url(std::string(url));
}
mind that the ctor is called after the object was created (= after the
new handler being called), so _this references a valid zval and obj an
allocated "object".
johannes
PS please eep the list in CC, others might learn or answer, too :-)
> Kind regards, Ingwie
> Am 30.09.2013 um 23:24 schrieb Johannes Schlüter
> <[email protected]>:
>
> > On Mon, 2013-09-30 at 21:12 +0200, Kevin Ingwersen wrote:
> >> Hello there.
> >>
> >> I am trying to read/write properties. I have found the
> read_property
> >> function as such:
> >>
> >> ZEND_API zval *zend_read_property(zend_class_entry *scope, zval
> >> *object, const char *name, int name_length, zend_bool silent
> >> TSRMLS_DC);
> >>
> >> But, what is a zend_bool? I trued passing false to it, but i got an
> >> error O.o so I think I sort of missed this a bit.
> >
> > A zend_bool is an unsigned char, see zend_types.h. I think we don't have
> > global true/false constants defined, so you can use 0 or 1 for false or
> > true.
> >
> > But I'm not sure I understood your question correctly. Please always
> > share some code and error messages.
> >
> >> I am essentially trying to store a resource into an object, and
> >> reading it off in another member function.
> >
> > What exactly are you trying to achieve? Are you creating an internal
> > class and want to store custom data (i.e. pointers to some library
> > stuff) in it? Then don't mess with resources but extend the zend_object
> > structure
> >
> > typedef struct {
> > zend_object zo; /* has to be the first element */
> > void *ptr; /* custom members */
> > } my_object;
> >
> > See http://talks.somabo.de/200903_montreal_php_extension_writing.pdf
> > slides 89-92.
> >
> > By overriding different property handlers these custom elements can be
> > exported to userland in nice ways.
> >
> > johannes
> >
>
--
PECL development discussion Mailing List (http://pecl.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php