Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Bernhard (4508)

Bernhard
  Bernhard.Schmalhofer@gmx.de
https://www.xing ... hard_Schmalhofer

A freelance software engineer with experience in webapplications, databases and bioinformatics.

A contributor to Parrot and the person behind Pipp.

A physicist who worked with third sound in Helium-III and CCD X-ray detectors.

barney on irc.perl.org
github: http://github.com/bschmalhofer [github.com]
LinkedIn:http://www.linkedin.com/in/bernhardschmalhofer [linkedin.com]
XING: https://www.xing.com/profile/Bernhard_Schmalhofer [xing.com]

Journal of Bernhard (4508)

Sunday June 07, 2009
06:45 AM

My first PHP extension

[ #39089 ]

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, http://pipp.org/, 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 http://devzone.zend.com/article/1021-Extension-Writing-Part-I-Introduction-to-PH P-and-Zend.

The first step is to build PHP 5.3 with development support. The source can be checked out from a CVS repository. See http://www.php.net/anoncvs.php 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-200901 15/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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.