PHP Extension Development

* Creating PHP5 extensions

. PHP5 extensions are the same as in PHP4
. ext_skel generates the basic skeleton

– Build PHP Extensions
src/php5/ext$ ./ext_skel –extname=util

1 $cd ..
2 $ vi ext/util/config.m4
3 $ ./buildconf –force
4 $ ./configure –with-util
5 $ make

(cont…)

– Files in your extension:
. Two code files:
php_yourext.h
php_yourext.c

. Two configuration files:
config.m4
config.w32

. Additional files:
.cvsignore
CREDITS
EXPERIMENTAL (if it’s still not stable)
package.xml (required for PECL extensions)
README

– config.m4
. PHP Dev is picky about coding style (watch your whitespace, align your PHP_ARG_ENABLE output)


dnl $id: $
dnl config.m4 for extension YOUREXT
PHP_ARG_ENABLE(yourext, disable, YourExt support,
[ --enable-yourext Enable YourExt], no)
if test "$PHP_YOUREXT" != "no"; then
AC_DEFINE(HAVE_YOUREXT, 1, [whether YourExt is present ])
PHP_NEW_EXTENSION(yourext, php_yourext.c, $ext_shared)
PHP_SUBST(YOUREXT_SHARED_LIBADD)
fi

– extension .h file


#idndef PHP_YOUREXT_H
#define PHP_YOUREXT_H

extern zend_module_entry yourext_module_entry;
#define phpext_yourext_ptr &yourext_module_entry
(…)

– layout of the .c file
. Header: License, Authors, CVS-Tag, ..
. Includes
. Structures and defines not in header
. Helper Functions
. PHP Functions
. Globals Handling
. MINFO
. MINIT, MSHUTDOWN (module)
. RINIT, RSHUTDOWN (request)
. Function table
. Module Entry

– Includes
Include path:
. <PHP Root>/
. <PHP Root>/Zend
. <PHP Root>/main
. <PHP Root>/ext/


#include "php.h"
#include "php_ini.h"
#include "php_yourext.h"

– Helper Functions
. Use static: if you need the function only in your .c file
. Use PHPAPI/YOUREXT_API: if you plan to use the functions in other extensions
. Use TSRMLS_xx as last function parameter: when dealing with PHP Data

– PHP functions
. Always use the layout below
. PHP is written in C not C++
Do not use // style C++ coments
Declarations are only allowed prior to code


/* {{{ proto type yourext_name(params)
Short description */

PHP_FUNCTION(yourext_name)
{
/* Local declarations */
/* Parameters parsing */
/* Actual code */
/* Return value */
}
/* }}} */
– Variable Types
. In PHP all values are zval’s
. PHP int is long in C

– Parsing parameters
. zend_parse_parameters is the easy way of parsing
. success is 0, failure is anything

– Use ZVAL_<type>() macros
. Nothing is freed or destructed
. Type is set to IS_<type>

– Setting the return value
. The return value is already allocated and IS_NUL

– Set return value and return
. Just like RETVAL_<type> but returning directly

– Some notes:
. You have to initialize option parameters
. Never use sprintf, use either snprintf or spprintf

– Dealing with arrays
. To initialize a zval as an array: array_init(zv); to return an array use: array_init(return_value)
. To add elements use the following:
. add_assoc_<type>(ar, key, …)
. add_index_<type>(ar, key, …) ????
. add_value_<type>(ar, key, …) ????

– Dealing with a HashTable
. PHP Arrays use SymbolTable, a special HashTable
. Numeric keys are treated as hash indices
. Non number indices are hashed
. SymbolTable keys include terminating
sizeof(key) vs. strlen(key)
. HashTable knows about its element count
. You can delete elements (SUCCESS/FAILURE) by key, hash index, symbol.
. You can lookup elements (SUCCESS/FAILTURE) by key, hash index, automatic preference of hash index over key (len=0), by symbol
. You can check for existence of elements (0/1) by key, hash index, automatic preference of hash index over key, by symbol
. Hash tables support a builtin foreach