Re: Writeing OO exteions for PHP 4.3
| From: | John Coggeshall | Date: | Fri, 03 Sep 2004 16:40:37 +0000 |
| Subject: | Re: Writeing OO exteions for PHP 4.3 | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-12563@lists.php.net to get a copy of this message | ||
If I understand you correctly you can put this pointer inside of a
resource container and return the resource container to user space as
the return value from url_init(). Then, from url_set_string() you accept
the resource as a parameter and extract the pointer to your CPP instance
of whatever class.
John
On Fri, 2004-09-03 at 10:54, Jan Gerritsen wrote:
> Hello,
>
> I want to write an OO extension in C++ for my PHP 4.3.8 under linux.
>
> I have already written some extensions in C for my own purposes, which
> introduce new functions to PHP.
> Studying the sources of ext/standard/dir.c, ext/ming/ming.c and the
> tutorial from J Smith (http://bugs.tutorbuddy.com/phpcpp/phpcpp/) I got
> already this far:
>
> I have managed to build and link the CPP sources as a shared object
> which can be loaded and called from PHP.
> I have introduced the new class to PHP using the following code in the
> PHP_MINIT_FUNCTION of my extension:
>
> static zend_class_entry *url_class_entry_ptr;
> static int le_url;
>
> function_entry url_class_functions[] = {
> PHP_FALIAS(url, url_init, NULL)
> PHP_FALIAS(set_string, url_set_string, NULL)
> {NULL, NULL, NULL}
> };
>
> static ZEND_RSRC_DTOR_FUNC(destroy_url)
> {
> if (rsrc->ptr) {
> delete (Url_class*) rsrc->ptr;
> rsrc->ptr = NULL;
> }
> }
> PHP_MINIT_FUNCTION(url)
> {
> zend_class_entry url_class_entry;
> INIT_CLASS_ENTRY(url_class_entry, "url", url_class_functions);
> url_class_entry_ptr = zend_register_internal_class(&url_class_entry
> TSRMLS_CC);
>
> le_url = zend_register_list_destructors_ex(destroy_url, NULL, "url",
> module_number);
>
> return SUCCESS;
> }
>
> This class can be created, and the functions (methods) can be called
> from PHP.
> Example:
> $url = new URL();
> $url->set_string("12345");
>
> Now my problem is, how can I save the pointer of my C++ class, created
> in the url_init function, and restore it in the other functions/methods:
> PHP_FUNCTION(url_init)
> {
> Url_class * url = new Url_class;
>
> / * code to save the address of url */
> }
>
> PHP_FUNCTION(url_set_string)
> {
> Url_class *url = NULL;
>
> /* code to fetch url */
>
> if( url != NULL ) {
> url->set_string(âtestâ);
> RETURN_TRUE;
> }
> RETURN_FALSE;
> }
>
> I could not work out how to do this, by studying the code of the OO
> extensions I mentioned.
> Can someone give me a hint, ore some example code, how to do such things?
>
> thanks
> Jan Gerritsen