Freelance Projects

Upeksha Wisidagama

Writing PHP Extensions

If you’re reading this tutorial, you probably have some interest in writing an extension for the PHP language. If not… well perhaps when we’re done you’ll have discovered an interest you didn’t know existed! - devzone.zend.com

If you have your PHP installed in a custom location, you need to add the bin directory to your PATH environment variable to avoid typing the full path. e.g. I have the bleeding edge PHP (PHP version 5.5.0) installed in /usr/local/php5/. To add the binaries to the $PATH,

export PATH=$PATH:/usr/local/php5/bin/

Preparing to Build the PHP Extension

Create a new directory for the new extension under source/ext. You need to create the following three files for a simple extension.

Create the config.m4 file.

‘config.m4’
1
2
3
4
5
6
7
PHP_ARG_ENABLE(upeksha_extension, whether to enable upeksha extension,
[ --enable-upeksha-extension   Enable Upeksha extension])

if test "$PHP_UPEKSHA_EXTENSION" = "yes"; then
  AC_DEFINE(HAVE_UPEKSHA_EXTENSION, 1, [Whether you have Upeksha extension])
  PHP_NEW_EXTENSION(upeksha_extension, upeksha_extension.c, $ext_shared)
fi

Create the upeksha.h file.

‘upeksha.h’
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef PHP_UPEKSHA_H

#define PHP_UPEKSHA_H 1

#define PHP_UPEKSHA_VERSION "0.1"
#define PHP_UPEKSHA_EXTNAME "upeksha_extension"

PHP_FUNCTION(say_name);

extern zend_module_entry upeksha_module_entry;
#define phpext_upeksha_ptr &upeksha_module_entry

#endif

Create the upeksha_extension.c file.

‘upeksha_extension’
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "upeksha.h"

// list of custom PHP functions provided by this extension
// set {NULL, NULL, NULL} as the last record to mark the end of list
static zend_function_entry upeksha_functions[] = {
    PHP_FE(say_name, NULL)
    {NULL, NULL, NULL}
};

// the following code creates an entry for the module and registers it with Zend.
zend_module_entry upeksha_extension_module_entry = {
    STANDARD_MODULE_HEADER,
    PHP_UPEKSHA_EXTNAME,
    upeksha_functions,
    NULL, // name of the MINIT function or NULL if not applicable
    NULL, // name of the MSHUTDOWN function or NULL if not applicable
    NULL, // name of the RINIT function or NULL if not applicable
    NULL, // name of the RSHUTDOWN function or NULL if not applicable
    NULL, // name of the MINFO function or NULL if not applicable
    PHP_UPEKSHA_VERSION,
    STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE(upeksha_extension)

// implementation of a custom say_name()
PHP_FUNCTION(say_name)
{
    RETURN_STRING("Upeksha Wisidagama", 1);
}

Next run phpize in the same directory.

Configuring the PHP Extension

Making and Installing the Upeksha PHP Extension

run make. You’ll get a message, ‘Libraries have been installed in: /home/uw/Downloads/php-5.5.0/ext/uw/modules’.

Load the extension by adding the following line to the php.ini file.

extension=/home/uw/Downloads/php-5.5.0/ext/uw/modules/upeksha_extension.so

Restart the Web Server.

Now, create the following php file with the function from the new extension.

‘hello.php’
1
2
3
<?php
echo say_name();
?>

Upeksha PHP Extension

See the new extension listed in phpinfo().

Upeksha PHP Extension in phpinfo()