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 php5
    % ./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
    PHP 5.1.0RC2-dev (cli) (built: Oct  2 2005 16:17:09)
    Copyright (c) 1997-2005 The PHP Group
    Zend Engine v2.1.0-dev, Copyright (c) 1998-2005 Zend Technologies

On Windows, things are similar, but not quite the same. Launch your Visual ...

Get Programming PHP, 2nd Edition 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.