PHP Extensions: building your extension part 1

15 Apr

Before going to discuss how many files you will need and the code, I’d like to tell that building extension is not a fun, so need a bit of extra concentration.

If someone ask me why I’ve learnt extension development?

I’ll simply say, for fun.

Although it’s my view point, however extension development come handy,if

  1. you want functionality that is not currently available in php
  2. you want to sold your product/application without giving away your code.
  3. functionality available in php but performance is slow,  this is because upperspace code take a bit longer than the code written in c that directly communicate.

Regardless of what is your requirements, in next few articles I would give you inner knowledge of how zend engine handle extension and how to build them.

To build a successful extension, you will need to have latest version of php, apache, pear and phpize install on your system.

Keep in mind that all the examples I’d give build under *nix family, so they will need some extra effort and little bit of changes in your code as well if you want them to build for windows.

Well, I was talking about the installation.

To have latest version of php on your system installed, open your console and write

$ sudo apt-get install php5

follow the instruction, this will install latest version of php.

Next for installing apache, write

$ sudo apt-get install apache2

and for pear

$ sudo apt-get install php-pear

and similarly for phpize, run the following command

$ sudo apt-get install php5-dev

You have now successfully install all the packages needed. Next step is to understand what files you will need to build an extension.

For building php extension, you will minimum of three files

  1. config.m4
  2. header file
  3. c source file

here c mean c language.

Make directory wherever you like and create the following three file

  1. config.m4
  2. php_sample.h
  3. sample.c

Now open config.m4 file and write the following code in it.

PHP_ARG_ENABLE(sample, Whether enable “sample” support,

[ –enable-sample         Enable “sample” support])

if test “$PHP_SAMPLE” = “yes”; then

AC_DEFINE(HAVE_SAMPLE, 1, [Sample exists])

PHP_NEW_EXTENSION(sample, sample.c, $ext_shared)


That’s it we have now created the configuration file. Phpize use this file for extension development.

Next step is to define our header file.

So open php_sample.h and write the following code in it.




#define PHP_SAMPLE_EXTNAME “sample”

#define PHP_SAMPLE_VERSION “1.0”



#include “config.h”


#include “php.h”

extern zend_module_entry sample_module_entry;

#define php_sample_extptr &sample_module_entry

This is our header file. Defining some micros, declaring a php function that will be defined later in the c source file and including some necessary header files.

Now its time to write the actual code that will having the functionality.

In your sample.c, write the following code.

#include “php_sample.h”

static function_entry php_sample_functions[] = {

PHP_FE(sample_hello, NULL);



zend_module_entry sample_module_entry = {

#if ZEND_MODULE_API_NO >= 20010901










#if ZEND_MODULE_API_NO >= 20010901







RETURN_STRING(“Hello world!”,1);


That’s it we have now define all the three file, now its time to build our first php extension.

Open your console and goto the directory where you have created these files using cd command.

Now write

$ sudo phpize

$ sudo ./configure -enable-hello

$ sudo make

these command will create few files and some directories. You extension file ( will reside under modules directory. Copy this file to your extension directory, make necessary configuration for enabling your extension in php.ini file and restart your apache with the following command.

$ sudo /etc/init.d/apache2 restart

And now open your php file and write the following code

echo sample_hello();

open yor php file using any browser, the browser must show the following contents.

Hello world!

If not, you may have missed something in the code.

Though lot of things aren’t explained here, however I’ll explain them as we move forward.

5 Responses to “PHP Extensions: building your extension part 1”

  1. Asim Zeeshan April 16, 2009 at 11:13 am #

    Wow, Good article.

  2. Faheem Abbas April 17, 2009 at 1:00 pm #


  3. Hassan August 1, 2009 at 5:05 pm #


    Thanks for you article. Currently i am using xampp and try to run

    $ sudo phpize

    $ sudo ./configure -enable-hello

    $ sudo make

    the above 3 lines from DOS command prompt. But the following error return

    ‘$’ is not recognized as an internal or external command,
    operable program or batch file.

    I think I can’t follow your ‘Open your console and goto the’ instruction.

    Please help me regarding this.

    Thanks again

  4. msdark September 8, 2009 at 8:06 pm #

    (Sorry for my english).

    Hassan xD … $ referes to promp on *Nix machin… if you try to run this simbol… you will have an error.. this is not a command.. and this how to is for *Nix machines not for WinUsers…

    Faheem Abbas … i have some problem compiling this.. in make i have a lot of errors and warning.. is in line 4 (PHP_FE(samle_hello,NULL); ) expected expression before ‘;’ token
    and in line 10 gcc tell ZEND_MODULE_HEADER undeclared … what happen here?
    I’m running ArchLinux with apache2 and php5 installed (including dev packages and pear)… but this doesn’t work


  5. Faheem Abbas September 9, 2009 at 5:03 am #

    When I was first trying to build my own extensions, I too encounter lot of errors and warnings. thus is what you get when you first start working on any language, tool. but by the time you fix them all. Don’t give it up, give some more tries, or read some more books.
    I have never ever given a try on win, but instead using *nix operating system.
    You need to have everything installed before all commands work for you.
    So take some time, read some more article available on the net, get an ebook available, you will soon get an idea.
    Once you successfully build your first extension, it will be like fun afterword.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: