My first PHP extension

Bernhard on 2009-06-07T11:45:24

PHP Extensions are usually written C and they usually export at least one constant, function, class, resource type or stream to the PHP userspace. So they are pretty much the same as XS-Modules in Perl 5. Extensive information about PHP extension can be found in Sara Golemons book 'Extending and Embedding PHP'.

The several hundred standard functions of PHP are implemented in terms of extensions. Pipp, , either needs to reimplement this massive count of functions, or support extensions natively. Being lazy, native support for extensions is the way to go. So let's see how to create a dummy PHP extension. The following is for Linux and is based on the tutorial .

The first step is to build PHP 5.3 with development support. The source can be checked out from a CVS repository. See for details and the password for anonymous CVS access.

  mkdir ~/first_php_extension
  cd ~/first_php_extension
  cvs -d :pserver:cvsread@cvs.php.net:/repository login
  cvs -d :pserver:cvsread@cvs.php.net:/repository checkout -r PHP_5_3 php5  
Buildconf performs some checks and creates a configure script for the GNU autotools. 're2c' is the parser generator used by PHP 5.3. It is not build dependency, as the generated C-files are in the repository. There is also a message about 'autotools 2.13' being recommended, but I simply ignore that.
  cd ~/first_php_extension/php5
  ./buildconf
For building extensions we need an installed PHP and it's associated helper scripts. Let's install our new PHP next to the source, so that the system PHP is left undisturbed.
  mkdir ~/first_php_extension/installed
  ./configure --prefix=/home/bernhard/first_php_extension/installed --enable-debug --enable-maintainer-zts --enable-embed
  make test
  make install
  export PATH=~/first_php_extension/installed/bin:$PATH
This should leave us with PHP 5.3 with debug support.
  bernhard@heist:~/first_php_extension/php5$ php --version
  PHP 5.3.0RC3-dev (cli) (built: Jun  7 2009 12:36:41) (DEBUG)
  Copyright (c) 1997-2009 The PHP Group
  Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

The next step is to create a dummy extension without specific functionality. The helper script 'ext_skel' creates a stub in the directory 'pipp_sample'.

  cd ~/first_php_extension/php5/ext
  ./ext_skel --extname=pipp_sample
For now I leave pipp_sample.c alone, but config.m4 needs to be edited. Instructions can be found in config.m4 itself, I ended up with uncommenting the lines 16, 18, 60.
  cd ~/first_php_extension/php5/ext/pipp_sample
  vi config.m4
The helper phpize takes info from the PHP installation and creates more files, including a configure script.
  phpize
Now the dummy extension can be configured and compiled.
  ./configure --enable-pipp_sample
  make test
A sanity test can be done with:
  php pipp_sample.php
Now let's copy the generated shared lib for use by the installed PHP.
  make install
The shared library could also be load by Pipp. Let's take a look at the function.
  bernhard@heist:~$ nm /home/bernhard/first_php_extension/installed/lib/php/extensions/debug-zts-20090115/pipp_sample.so | grep ' U '
  U php_info_print_table_end
  U php_info_print_table_header
  U php_info_print_table_start
  U spprintf
  U zend_parse_parameters
The undefined functions are provided by the PHP extension API. These are also the functions that need to be implemented for Pipp.