Introduction to Extensions

Extensions are pre-compiled codes or libraries which enable specific functions to be used in your PHP code. These extensions may be either a PHP extension or a Zend  extension.We can see extensions in php.ini file.

; syntax:

; extension=modulename.extension

; For example, on Windows:

; extension=msql.dll

; under UNIX:


Why we need to create Extensions

  • To create our own efficient and high performance PHP code which adds  missing features to the language.
  • When there are  limitations in PHP where we cannot make call to some library or OS specific calls.
  • When we have our own set of intelligent business logic which we want to sell and execute but do not want this to be viewed by others.
  • When we want to make PHP software to behave in a superior manner

How to create an Extension

The following system requirements need to be present to  create your own PHP extensions:

  • Text editor
  • PHP
  • Source code file and
  • C compiler.

Required files:

config. m4: The first required file to store the basic configuration data used by PHP to compile your custom extension.


[Whether to enable the “my_code” extension],

[-enable-my_code  Enable “my_code” extension support])

if test $PHP_my_code != “no”; then // 1st argument declares the module

PHP_SUBST(my_code_SHARED_LIBADD) // 2nd tells what all files to compile

PHP_NEW_EXTENSION(my_code,my_code.c,$ext_shared)  // $ext_shared is counterpart of PHP_SUBST()


In this case my_code.c is source code file and its content is :


#include “config.h”


#include “php.h”

#define PHP_MY_CODE_VERSION “1.0”

#define PHP_MY_CODE_EXTNAME “my_code”

extern zend_module_entry my_code_module_entry;

#define phpext_my_code_ptr &my_code_module_entry

// declaration of a custom my_code_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_code_functions[] = {

PHP_FE(my_code_function, NULL)



// the following code creates an entry for the module and registers it with Zend.

zend_module_entry my_code_module_entry = {

#if ZEND_MODULE_API_NO >= 20010901





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

Add to php.ini file

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

Testing the Extension

You can test your PHP extension by typing the following command:

  1. $ php -r “echo my_code_function();”

Related Posts