Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98028 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 44402 invoked from network); 29 Jan 2017 22:48:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Jan 2017 22:48:09 -0000 Authentication-Results: pb1.pair.com smtp.mail=johannes@schlueters.de; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=johannes@schlueters.de; sender-id=unknown Received-SPF: error (pb1.pair.com: domain schlueters.de from 84.19.169.162 cause and error) X-PHP-List-Original-Sender: johannes@schlueters.de X-Host-Fingerprint: 84.19.169.162 unknown Received: from [84.19.169.162] ([84.19.169.162:42834] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 35/B8-06271-4217E885 for ; Sun, 29 Jan 2017 17:48:05 -0500 Received: from [192.168.2.34] (ppp-46-244-187-33.dynamic.mnet-online.de [46.244.187.33]) by mail.experimentalworks.net (Postfix) with ESMTPSA id B22AB50ADF; Sun, 29 Jan 2017 23:48:01 +0100 (CET) Message-ID: <1485730077.15858.22.camel@kuechenschabe> To: Torsten Rosenberger Cc: internals@lists.php.net Date: Sun, 29 Jan 2017 23:47:57 +0100 In-Reply-To: <1484727448.3119.8.camel@toro.home> References: <1484727448.3119.8.camel@toro.home> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.10.4-0ubuntu2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] writing extensions best practice, pass an object as parameter From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) On Wed, 2017-01-18 at 09:17 +0100, Torsten Rosenberger wrote: > Hello List > > I'm new to C and writing extensions > I hope this ist the right list. > > I want to port the PEAR HTML_Template_Sigma Class to an PHP > extension. I startet with the old book 'Building custom PHP Extensions' > found now newer one. > > At the moment i have some class functions in my extension as they are in > the PHP Class. > > in the PHP Class are some callback functions registert > > $this->setCallbackFunction('e', array(&$this, '_htmlentities')); Using a reference here is useless. Just put $this in the aray. > $this->setCallbackFunction('u', 'urlencode'); > > the easier callback's to a php function i managed with this code > > > zval tplFunction; > ZVAL_STRING(&tplFunction,"u"); > zval callback; > ZVAL_STRING(&callback,"urlencode"); > > zend_call_method_with_2_params ( obj,Template_Sigma, NULL, > "setcallbackfunction", NULL, &tplFunction, &callback); > > > but how can i pass the object from a private method of the PHP extension > class to the setcallbackfunction as it is down in PHP with > > array(&$this, '_htmlentities') > > > > So i try doing some bench :( > Testing with php7. > > i call the class constructor from the extension 1000 times and also the > same with the PHP class. > > $x = new HTML_Template_Sigma("dir","dir_cache"); I don't fully understand what exactly you want to do. If you want to pass an array with two values to zend_call_method you have to build the array (array_init, add_next_index_zval, add_next_index_stringl etc.) and eventually free this again. For a private member you of course have to mind the context which is given as class entry via the second argument to zend_call_method_with_2_params If you want to receive a callback from a user see the "f" modifier for zend_parse_paramters, when storing this you probably have to increase a reference count on some object. In https://github.com/php-test-helpers/php-test-helpers/blob/master/test_helpers.c#L396 I did this, while that code might not work in PHP 7. > so the PHP Class with opcache ist equal faste as my extension. > My goal was to write the class as an extension to get faster. > > > > > The last question should I use call_user_function or write this function > in C. > > for example > the private methode _jsEscape uses the PHP strtr and the second > parameter is an array so the internal php_strtr_array function is > called. Should I instead call the php_strtr function multiple times ? If the extension primarily is just calling userspace-exported functions there's not much performance gain to win. If you can do more complex operations in C there can be some gain, though. johannes