Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:26739 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 71185 invoked by uid 1010); 30 Nov 2006 06:43:34 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 71170 invoked from network); 30 Nov 2006 06:43:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Nov 2006 06:43:34 -0000 Authentication-Results: pb1.pair.com header.from=pollita@php.net; sender-id=unknown; domainkeys=good Authentication-Results: pb1.pair.com smtp.mail=pollita@php.net; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain php.net from 140.211.166.39 cause and error) DomainKey-Status: good X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: pollita@php.net X-Host-Fingerprint: 140.211.166.39 osu1.php.net Linux 2.4/2.6 Received: from [140.211.166.39] ([140.211.166.39:57584] helo=osu1.php.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6A/2C-00608-69D7E654 for ; Thu, 30 Nov 2006 01:43:34 -0500 X-DomainKeys: Ecelerity dk_sign implementing draft-delany-domainkeys-base-01 DomainKey-Signature: q=dns; a=rsa-sha1; c=nofws; s=mx; d=php.net; h=From:Subject:To:Date; b=tT+QliYLtJ7yeEaF1I7ewDA6BvXCN6XNFqau3rM2Kp+MuQwnzx6H0KS6S8hYmCOX /eXOG1TRyq8TiCRBU2IlCdLfQ3JQrJyNg3e8pcDMZaUEHolaFbWeRfsGUpKxqThs Authentication-Results: osu1.php.net smtp.user=pollita; auth=pass (LOGIN) X-Host-Fingerprint: 69.12.155.129 unknown Received: from [69.12.155.129] ([69.12.155.129:1170] helo=[172.31.5.145]) by osu1.php.net (ecelerity 2.1.1.11-rc1 r(13363/13364M)) with ESMTPSA (cipher=AES256-SHA) id 8F/BA-12683-F5E7E654 for ; Wed, 29 Nov 2006 22:46:55 -0800 Message-ID: <456E7D53.90606@php.net> Date: Wed, 29 Nov 2006 22:42:27 -0800 User-Agent: Thunderbird 1.5.0.8 (Windows/20061025) MIME-Version: 1.0 To: "Mauro N. Infantino" CC: internals@lists.php.net References: <008601c71245$8681ded0$ec01010a@intranet.db> In-Reply-To: <008601c71245$8681ded0$ec01010a@intranet.db> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: Cross-extension resource From: pollita@php.net (Sara Golemon) > I'm writing an extension in order to achieve better performance in a > specific module from our application. Right now, I'm trying to use an > already established mysql connection (with mysql_connect) in our extension, > so we don't have to connect to the database twice, since that connection is > used in the PHP code. I don't need PHP's mysql API, since I can do > everything in C, I just want the connection ID. I've read Sara's (great... > GREAT) book, but I couldn't find any specific solution to this situation. Is > this possible? Is there a "best" way? > Surprising how often this question comes up.... /* True global to store local copy of mysql's le_link */ static int local_mysql_le = -1; PHP_RINIT_FUNCTION(myext) { if (local_mysql_le == -1) { local_mysql_le = zend_fetch_list_dtor_id("mysql link"); } return SUCCESS; } A few notes about this solution: (1) I'm doing this in RINIT because, prior to PHP 5.1, there's no way to enforce load order for shared extensions, so it's possible that your ext will load prior to mysql and therefore the list ID won't have been registered during the time of MINIT. If you know that MySQL will always be loaded prior to your extension (either because of local policy or because you're only targeting 5.1 or later -- which has module dependencies), then you can do it once in MINIT and be done with it. (2) The name "mysql link" is case sensitive and must match the resource type name you're looking for precisely. There's also no guard against the (unlikely) possibility that some other extension registers a resource named "mysql link" which isn't actually a MySQL link. (3) You should guard against the possibility that no matching list id will be found for that name (perhaps MySQL isn't loaded), so be sure to put some error checking in there... Hint: This function returns 0 on failure. (4) You'll probably need to fetch "mysql link persistent" as well... I think this function is covered in Appendix A, but the cat is on my lap and I can't reach the bookshelf from here.... -Sara