How to create custom PHP extension?

Required Tools

In order to create custom PHP extension, requires the following build tools
  • C/C++ compiler (For example, GCC).
  • phpize which is a shell script to prepare PHP extension for compiling.
  • php-config which is a simple shell script for obtaining information about the installed PHP configuration.
On RPM based systems, required packages can be installed using following command:
$ yum install gcc php php-devel

On Debian based systems, required packages can be installed using following command:
$ apt-get install gcc php php5-dev

Create config.m4 file 

PHP_ARG_ENABLE(my_extension, whether to enable my extension, 
[ --enable-my-extension Enable my extension])
if test "$PHP_MY_EXTENSION" = "yes"; then
 AC_DEFINE(HAVE_MY_EXTENSION, 1, [Whether you have my extension])
 PHP_NEW_EXTENSION(my_extension, my_custom_extension.c, $ext_shared)
fi


The config.m4 file for an extension tells the UNIX build system 
  • what configure options your extension supports
  • what external libraries and includes you require
  • what source your require

This config.m4 stores the basic configuration data used by the PHP 
to compile your custom extension.

Create my_custom_extension.c 

#ifdef HAVE_CONFIG_H

#include "config.h"
#endif
#include "php.h"

#define PHP_MY_EXTENSION_VERSION "1.0"
#define PHP_MY_EXTENSION_EXTNAME "my_extension"

extern zend_module_entry my_extension_module_entry;
#define phpext_my_extension_ptr &my_extension_module_entry
// declaration of a custom my_function()
PHP_FUNCTION(my_function);
// list of custom PHP functions provided by this extension
// set {NULL, NULL, NULL} as the last record to mark the end of list
static function_entry my_functions[] = {
PHP_FE(my_function, NULL)
{NULL, NULL, NULL}};

// the following code creates an entry for the module and registers it with Zend.
zend_module_entry my_extension_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_MY_EXTENSION_EXTNAME,
my_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
#if ZEND_MODULE_API_NO >= 20010901
PHP_MY_EXTENSION_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(my_extension)
// implementation of a custom my_function()
PHP_FUNCTION(my_function) {
RETURN_STRING("A line from my custom php extension", 1);
}

Building the extension

$ phpize
$ ./configure
$ make install
  • The phpize command will generate additional configuration  files, required to compile your PHP extension 
  • you can reverse this process anytime by typing the “phpize –clean” command
  • After running each of these commands, you should have a my_custom_extension.so file in PHP extensions directory.
If don’t know the name of that directory, you can check it’s name by typing:
$ php-config | grep extension-dir

Configure php.ini

The last thing you need to do, is to add the following line to your php.ini to load your extension on PHP startup:
extension=my_extension.so

Testing the extension

You can test your PHP extension by typing the following command:
$ php -r "echo my_function();"

If everything was fine, the output will be:
A line from my custom php extension


3 comments:

  1. If you want to learn wordpress then start from here.I have create a tutorial with screen shots so that you can easy understand.
    Learn wordpress theme development from scratch

    ReplyDelete
  2. Nice blog...Very useful information is providing by ur blog. find Great beginning php tutorials Very clear and helpful for beginners.

    ReplyDelete
  3. The data gave here was of high caliber and valuable. A debt of gratitude is in order for the instructive post gave. Website Design Company Bangalore | Website Development Company Bangalore

    ReplyDelete