Building Your First Extensions

This section walks you through the steps of building your first extension, from design through testing. Most extensions are created by writing a file that defines the functions the extension will have, building a skeleton from that, and then filling in the C code that does the actual work of the extension. This section doesn’t cover advanced topics such as returning complex values or managing memory—we’ll talk about those later, after you have the basics down.

Command-Line PHP

Unless your extension can really be tested only through the Web, it is much easier to debug and quickly test your code through the command-line version of PHP (also sometimes referred to as the CGI version of PHP). To build the command-line version, do something like this:

% cd php4
% ./configure --with-mysql=/usr --with-pgsql --with-zlib --with-config-file=/etc   
% make
# make install

This will put a php binary in your /usr/local/bin directory. The configure line above adds MySQL, PostgreSQL, and zlib support. While you don’t need them to develop your extension, they won’t get in the way, and it is a good idea to have a php binary that can run complex web applications directly from the command line.

Just to make sure it worked, test it:

% /usr/local/bin/php -v
4.2.0-dev

Planning Your Extension

As much as you probably just want to dive in and start coding, a little bit of planning ahead of time can save you a lot of time and headaches later. The best way to plan your extension ...

Get Programming PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.