===== Common comments from reviewing PECL proposals =====
While reviewing PECL proposals there are some common issues which can be observed. This page collects some of those to provide extended information and saving reviewers from explaining those multiple times. This is different from other sections on this wiki a it focusses on review perspective.
**Note to documentation team:** When adding this information to the manual please keep a reference on this page!
====== php_$extname.h should be minimal ======
When reviewing extensions one can often see a long //php_$extname.h// file, which is bad. The //php_$extname.h// header should only contain a limited set of declarations. It should not include library or system headers. At most, it should contain declarations for the extension's module functions (like PHP_MINIT_FUNCTION), a module entry pointer and a version definition.
If needed it may contain some public C-level APIs for other extensions. If there's a larger set of exported functionality these should get an extra header though. When providing such APIs the //php_$extname.h// still shall not include other headers.
The reason for this is that for a static build of the extension as part of the main PHP build the PHP configure script will create //main/internal_functions.c// and //main/internal_functions_cli.c// which will include the //php_$extname.h// headers of all activated extensions for creating the extension startup structure. Including too many declarations there not only increases compile times but also increases the possibility of conflicts between declarations as different extensions might use the same sourcelevel symbols for different purpose.
A good starting point for a //php_$extname.h// is this:
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Ted User |
+----------------------------------------------------------------------+
*/
#ifndef PHP_EXTNAME_H
#define PHP_EXTNAME_H
extern zend_module_entry extname_module_entry;
#define phpext_extname_ptr &extname_module_entry
#endif
====== Don't use C99 for portability reasons ======
C99 is more than 10 years old. Still compiler vendors (most notably Microsoft Visual C) have only limited support of C99 features. Using C99 in an extension code can therefore limit the portability. C99 features include
* inline functions
* intermingled declarations and code: variable declaration is no longer restricted to file scope or the start of a compound statement (block), similar to C++
* several new data types, including long long int, optional extended integer types, an explicit boolean data type, and a complex type to represent complex numbers
* variable-length arrays
* support for one-line comments beginning with %%.//%%., as in BCPL or C++
* new library functions, such as snprintf
* new header files, such as stdbool.h, complex.h, tgmath.h, and inttypes.h
* type-generic math functions
* improved support for IEEE floating point
* designated initializers
* compound literals
* support for variadic macros (macros with a variable number of arguments)
* restrict qualification allows more aggressive code optimization
* universal character names, which allows user variables to contain other characters than the standard character set
(Source: [[http://en.wikipedia.org/wiki/C99#Design]])
A common mistake is the second item. Note that the macro TSRMLS_FETCH() includes a variable declaration. This macro should be the last item in the variable declaration. Code like this is invalid, according to this rule:
static long myext_do_something() {
long a, b;
a = myext_get_a();
TSRMLS_FETCH();
b = myext_get_b(TSRMLS_C);
long sum = a+b;
return sum;
}
This function might be written like this:
static long myext_do_something() {
long a, b;
long sum;
TSRMLS_FETCH();
a = myext_get_a();
b = myext_get_b(TSRMLS_C);
sum = a+b;
return sum;
}
====== Don't use zend_error ======
//zend_error()// should only be used inside the engine. Inside PHP extensions only PHP's error functions shoudl be used. Typically //php_error_docref()// is the best choice. //php_error_docref()// will extend the error message by extra information, like the current function name and properly escape output where needed. //zend_error()// is used by the Zend Engine due to PHP's modular architecture where the Zend Engine and TSRM should be compilable without other parts of PHP. The engine therefore can not use PHP level APIs. This limitation does not exist in extensions.
====== If zend_parse_parameters() fails do a simple return ======
The //zend_parse_parameters()// function is used to access the function parameters from the executors stack. It will automatically provide an error message to the user. By doing a plain //return;// the code also will keep the predefined value for the functions //return_value//, which is //NULL//. Returning NULL to the user is the default behaviour documented in the manual:
> **Note:** If the parameters given to a function are not what it expects, such as passing an array where a string is expected, the return value of the function is undefined. In this case it will likely return **NULL** but this is just a convention, and cannot be relied upon.
(Source: [[http://php.net/functions.internal]])
Even though this is a convention this rule should be followed unless there are good reasons.
Those are wrong:
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
php_error_docref(NULL, TSRMLS_CC, E_WARNING, "Error while parsing parameters"):
return;
}
This form is suggested:
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
See also README.PARAMETER_PARSING_API in the PHP source tree for your version of PHP.
====== RINIT/RSHUTDOWN should be as small as possible ======
RINIT/RSHUTDOWN are being called on every request handled by PHP, independently from whether the functionality is needed or not. Often it is a good alternative to delay initialisation of external libraries etc. which is commonly done in RINIT till the script access this for the first time or doing it globally in MINIT. RSHUTDOWN functionality can often be bound to resource/object destructors.
====== Empty RINIT/RSHUTDOWN functions should be removed ======
If RINIT/RSHUTDOWN functions aren't needed they should be removed and the entries of the module entry structure should be set to NULL. Besides having less code = less chance of bugs this brings a tiny performance improvement as it saves 2 indirect function calls per request, which PHP would happen to execute on every request. A module structure might look like this:
zend_module_entry myext_module_entry = {
STANDARD_MODULE_HEADER,
"myext",
myext_functions,
PHP_MINIT(myext),
PHP_MSHUTDOWN(myext),
NULL,
NULL,
PHP_MINFO(myext),
PHP_MYEXT_VERSION,
STANDARD_MODULE_PROPERTIES
};
If not needed one might also remove the function table, MINIT and MSHUTDOWN if those aren't needed, but as they are only accessed during the initial PHP startup, not for every request, this is less relevant.
Also see the previous item.
====== PECL site conformity ======
A few conformity checks are done on the tarball upload, the PECL site will check for
. presence of LICENSE or COPYING in the package.xml, add it to the root dir with the line like this
. for version string compliance in package.xml and the extension source code. The following should be defined in one of the extension header files
#define PHP_EXTNAME_VERSION "1.2.3"
This macros has to be used within your extname_module_entry to indicate the extension version. The version string in both package.xml and the defined macros have to match. The work of adding that macros could be already done when using ext_skel on PHP versions as of 5.4.21 or 5.5.5.