calling user functions


// retval_ptr is used for the return value of the function call. zval *retval_ptr; // params for the function call. zval ***params; // just one parameter int n_params = 1; // status is used for detecting function call's return status int status; // the function call (callback) zval *cb_array; if (!cb_array || !zend_is_callable(cb_array, IS_CALLABLE_CHECK_NO_ACCESS, NULL TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "First argument is expected to be a valid callback"); zval_dtor(value); Z_TYPE_P(value) = IS_NULL; return; } params = safe_emalloc(sizeof(zval **), n_params, 0); params[0] = &value; // You can replace EG(function_table) with the function table of a class entry if you're calling a class method. /* function table, object ptr, cb_array = callback, n_params, params */ status = call_user_function_ex(EG(function_table), NULL, cb_array, &retval_ptr, n_params, params, 0, NULL TSRMLS_CC); // free the params efree(params); // copy retval zval to the zval we need to use. if (status == SUCCESS && retval_ptr != NULL) { if (retval_ptr != value) { zval_dtor(value); COPY_PZVAL_TO_ZVAL(*value, retval_ptr); } else { zval_ptr_dtor(&retval_ptr); } } else { zval_dtor(value); Z_TYPE_P(value) = IS_NULL; }

Extension Skeleton

If you encountered this error while running make test or loading extension:

PHP Warning:  PHP Startup: Invalid library (maybe not a PHP library) 'phpsgi.so' in Unknown on line 0

Be sure to check you defined this:

#if COMPILE_DL_FOO
ZEND_GET_MODULE(foo)
#endif

Extension Skeleton

To create an extension, you will need at least three files:

  • config.m4
  • ext_foo.c
  • ext_foo.h

Here is a initial content of config.m4:

PHP_ARG_ENABLE(foo, whether to enable foo extension support,
  [--enable-foo Enable foo extension support])
PHP_NEW_EXTENSION(foo, php_foo.c, $ext_shared)

here is the content of php_foo.h:

#ifndef _PHP_FOO_H
#define _PHP_FOO_H
#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif

#ifdef ZTS
    #warning php_ext_uv module will *NEVER* be thread-safe
    #include <TSRM.h>
#endif

#include <php.h>

extern zend_module_entry foo_module_entry;

PHP_FUNCTION(foo_hello);

#endif

Here is the content of php_foo.c, a minimal extension source code must contains a zend_module_entry, And you usually need to define zend_function_entry to declare your extension functions:

#include "php_foo.h"

PHP_MINIT_FUNCTION(foo);
PHP_MSHUTDOWN_FUNCTION(foo);
PHP_MINFO_FUNCTION(foo);

#if COMPILE_DL_FOO
ZEND_GET_MODULE(foo)
#endif

static const zend_function_entry foo_functions[] = {
    PHP_FE(foo_hello, NULL)
    PHP_FE_END
};

zend_module_entry foo_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    "Foo",
    foo_functions, // where you define your functions
    PHP_MINIT(foo),
    PHP_MSHUTDOWN(foo),
    NULL, // PHP_RINIT(foo)
    NULL, // PHP_RSHUTDOWN(foo)
    PHP_MINFO(foo),
#if ZEND_MODULE_API_NO >= 20010901
    "0.1",
#endif
    STANDARD_MODULE_PROPERTIES
};

PHP_MINIT_FUNCTION(foo) {
    return SUCCESS;
}

PHP_MSHUTDOWN_FUNCTION(foo) {
    return SUCCESS;
}

PHP_MINFO_FUNCTION(foo) {
}

PHP_FUNCTION(foo_hello) {
    RETURN_TRUE;
}

Compile it!

phpize
./configure
make

PHP Extension Book Extension Cookbook!

Powered By