This is a short tutorial on how to use the PHP: Abstract Extension API and Dependency Interface.
You can find details about he status of the project here. The source code is available at the git repository : git://github.com/vpj/PHP-Extension-API.git, and some sample code which uses the interface : git://github.com/vpj/PHP_EXT_API_Tests.git
How to register APIs
You should create a structure with the API functions.
| struct _SAMPLE_EXT_API {
int (*add)(int, int);
int (*multiply)(int, int);
};
typedef struct _SAMPLE_EXT_API SAMPLE_EXT_API; |
Then you should register the API during module initialization
| zend_ext_api_register("calculator", "1.0.0.0", (void *)&ext, sizeof(ext)); |
calculator is the name of the extension and 1.0.0.0 is the version in the major.minor[.build[.revision]] format. It is possible to register multiple APIs if there are multiple versions, to support backward compatibility. For example:
| zend_ext_api_register("calculator", "1.0", (void *)&ext_old, sizeof(ext_old));
zend_ext_api_register("calculator", "1.1", (void *)&ext_new, sizeof(ext_new)); |
Getting the required APIs
APIs could be retrieved using a callback function or using zend_ext_api_get. Callbacks are called after module initialization but before RINIT. Callbacks should be used when the dependent extension requires some extension during module initialization. APIs cannot be retrieved during MINIT, since it will depend on the order in which APIs are registered. Therefore, the dependent extension could set up the callback function during MINIT and it will be called once all extensions are initialized.
Here’s an example of a callback function
| typedef struct _SAMPLE_EXT_API {
int (*add)(int, int);
int (*multiply)(int, int);
} SAMPLE_EXT_API;
void my_callback(void *p_api, char *ext_name, uint version)
{
char *version_text;
SAMPLE_EXT_API *api = (SAMPLE_EXT_API *)p_api;
zend_ext_api_version_toa(version, &version_text);
php_printf("API Callback: %s - %d\n", ext_name, version_text);
php_printf("\tsum(243, 34) = %d\n", api->sum(243, 34));
} |
Callback should be registered in MINIT
| zend_ext_api_set_callback("calculator", "1.0.0.0", my_callback); |
Recent Comments