Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:14628 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 17418 invoked by uid 1010); 4 Feb 2005 01:37:02 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 17398 invoked from network); 4 Feb 2005 01:37:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Feb 2005 01:37:02 -0000 X-Host-Fingerprint: 64.233.184.201 wproxy.gmail.com Linux 2.4/2.6 Received: from ([64.233.184.201:45366] helo=wproxy.gmail.com) by pb1.pair.com (ecelerity HEAD (r4105:4106)) with SMTP id 82/D5-05104-EB1D2024 for ; Thu, 03 Feb 2005 20:37:02 -0500 Received: by wproxy.gmail.com with SMTP id 49so196167wri for ; Thu, 03 Feb 2005 17:36:59 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=FwBIv6suY7xyBh9+qzY1GKgzmTru8ejH3keXt4dzeQwAewfjvmFo0CyaGtEKjAhizzNz2bzdfwGe6DaGEDYiTQ7qqT6uCIJmTIqJeyR47+e0vMjbswCaTx4Ke767man6jOAwAIVW0NbTw+aIXRfDRRDh3m4dy5oSu5aExmOv7KA= Received: by 10.54.19.29 with SMTP id 29mr372762wrs; Thu, 03 Feb 2005 17:36:58 -0800 (PST) Received: by 10.54.59.22 with HTTP; Thu, 3 Feb 2005 17:36:58 -0800 (PST) Message-ID: <4e89b426050203173642c9c28b@mail.gmail.com> Date: Thu, 3 Feb 2005 20:36:58 -0500 Reply-To: Wez Furlong To: Derrell.Lipman@unwireduniverse.com Cc: internals In-Reply-To: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: Subject: Re: [PHP-DEV] how to make nasty code less nasty (obtaining sqlite db handle) From: kingwez@gmail.com (Wez Furlong) There is no official way to do this kind of thing between arbitrary extensions. When extensions that are written to share this information, they typically export an XXX_API function that you can link against to fetch the data. What you've done works, so that's fine, although there is a risk of crashing if someone passes the wrong type of resource in. If you're thinking of distributing your extension, we could add a more official API for you to use, although that doesn't exist in older versions of the extension, etc. etc. Some time back, I proposed a framework (and even a patch IIRC) that allowed this kind of thing at run-time; despite "we the extension developers" liking the idea, there was some negative feedback from andi/zeev so it never went further. Search the php-dev archives for my name and "interfaces" for more on that if you're interesting in this seeing light in PHP 5.2. --Wez. On Thu, 03 Feb 2005 17:03:45 -0500, Derrell.Lipman@unwireduniverse.com wrote: > I've written an extension which needs to accept a Resource which is an SQLite > database handle, and call a C function which will be using that db handle to > issue queries. What I've done works, but is kinda nasty because there doesn't > appear to be a clean way to obtain that database handle nor, from what I can > tell, determine the resource type to expect. Is there a nicer way of doing > this? > > PHP_FUNCTION(my_PHP_function) > { > int id; > int dataLen; > int resourceType; > char * pData; > zval * zdb; > void * hDB; > void ** phDB; > > /* Ensure we got the correct number of parameters */ > if (ZEND_NUM_ARGS() != 2) > { > WRONG_PARAM_COUNT; > } > > /* Retrieve the parameters */ > if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, > "rs", > &zdb, &pData, &dataLen) == FAILURE) > { > return; > } > > /* > * Voodoo to retrieve the sqlite database handle from the resource. > * How do I validate that resourceType is reasonable? > */ > id = zdb->value.lval; > if ((phDB = zend_list_find(id, &resourceType)) == NULL) > { > return; > } > > /* > * This is nasty too. We "know" that the first field in the private > * structure is the database handle. Just extract it. > */ > hDB = *phDB; > > RETURN_STRING(my_C_function(hDB, pData), TRUE); > } > > Thanks, > > Derrell > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >