Re: exposing C structs

From: Date: Fri, 27 May 2011 16:15:02 +0000
Subject: Re: exposing C structs
References: 1  Groups: php.pecl.dev 
Request: Send a blank email to pecl-dev+get-8307@lists.php.net to get a copy of this message
Hi, On Tue, 2011-05-24 at 18:06 +0100, "Neuhauser, Roman (GE Capital, consultant)" wrote: > typedef struct DapiDate { ... > typedef struct DapiTime { ... > typedef struct DapiDateTime { ... Without looking deep into this: Can't you use PHP's DateTime class here instead of using an additional representation of dates here? > i have some experience with extensions, but never needed to expose > data members. the PHP classes corresponding to these structs, as well > as their instances, must support reflection. > > so, let's say i have this C++ code: > > struct dapic_DapiDate { > zend_object std; > DapiDate impl; > }; > > PHP_MINIT_FUNCTION(dapic) > { > zend_class_entry ce; > INIT_CLASS_ENTRY(ce, "DapiDate", dapic_no_methods); > dapic_DapiDate_ce = zend_register_internal_class(&ce TSRMLS_CC); > dapic_DapiDate_ce.create_object = > dapic_create_handler<dapic_DapiDate>; > ... > } > > what's the minimum required to > > 1) expose the dapic_DapiDate->impl members in userspace (r/w)? > 2) make them visible to ReflectionClass? As you're mapping the struct members the best might be to override the read/write_property object handlers. Basically you do dapic_DapiDate_ce.read_property = dapic_date_read_property; dapic_DapiDate_ce.write_property = dapic_date_read_property; And implement the handlers with these signatures: zval *dapic_date_read_property(zval *object, zval *member, int type TSRMLS_DC) void dapic_date_write_property(zval *object, zval *member, zval *value TSRMLS_DC) ideally you also implement the get_properties handler. So print_r() and others work. While that's not a strict requirement for work. I don't have a short example at hand but going through the code should give you some. johannes

« previous php.pecl.dev (#8307) next »