Chapter 14. Extending PHP
This chapter shows you how to write
C language extensions to PHP. Although most functionality can be
written in the PHP language, sometimes you need the extra speed and
control you get from the C API. C code runs an order of magnitude
faster than most interpreted script code, and it is also the
mechanism for creating the thin middle layer between PHP and any
third-party C library.
For example, to be able to talk to the MySQL database server, PHP
needs to implement the MySQL socket protocol. It would be a lot of
work to figure out this protocol and talk to MySQL directly using
fsockopen( ) and fputs( ) from
a PHP script. Instead, the same goal can be accomplished with a thin
layer of functions written in C that translate
MySQL's C API, implemented in the
libmysqlclient.so library included in MySQL,
into PHP language-level function calls. This thin layer of functions
is known as a PHP extension. PHP extensions do
not always have to be a layer between PHP and some third-party
library, however. An extension can instead completely implement some
feature directly (for example, the FTP extension).
Before we get into the details of writing extensions, a note of
caution. If you are just learning PHP and do not have any sort of C
programming background, you should probably skip this chapter.
Extension writing is an advanced topic, and it is not for the faint
of heart.
14.1. Architectural Overview
There are two kinds of extensions that
you can write: PHP extensions and
Zend
extensions. We will focus on PHP extensions here. Zend extensions are
lower-level extensions that somehow modify the very core of the
language. Opcode cache systems such as
APC, Bware afterBurner, and ZendCache are Zend extensions. PHP
extensions simply provide functions or objects to PHP scripts. MySQL,
Oracle, LDAP, SNMP, EXIF, GD, and ming are all examples of PHP
extensions.
Figure 14-1 shows a diagram of a
web server with PHP linked in. The
web server layer at the top handles incoming HTTP requests and passes
them to PHP via the Server Abstraction API (SAPI). The
"mysql",
"ldap", and
"snmp" boxes represent loadable PHP
extensions, the kind you'll learn how to build in
this chapter. TSRM is the Thread Safe
Resource Manager layer, which helps simplify thread-safe programming.
The PHP Core contains many of the nonoptional core features of PHP,
and the PHP API contains the PHP-specific API functions used by both
the core and the PHP extensions. Finally, there is the Zend engine,
which runs scripts through a two-pass mechanism, first generating a
set of opcodes and then executing them. A PHP extension uses the Zend
extension API to receive arguments from function calls and return
values back.
Figure 14-1. Structure of a PHP-linked web server
![Previous](../gifs/txtpreva.gif) | ![Home](../gifs/txthome.gif) | ![Next](../gifs/txtnexta.gif) | 13.5. Performance Tuning | ![Book Index](../gifs/index.gif) | 14.2. What You'll Need |
Copyright © 2003 O'Reilly & Associates. All rights reserved.
|