A PHP extension allows you to connect almost any C/C++ code you want to PHP. This is a 4-part tutorial on how to write an extension:
- Setting Up PHP – compiling PHP for extension development
- Hello, world! – your first extension
- Working with the API – the PHP C API
- Classes – creating PHP objects in C
Almost all of the code examples in this tutorial are available on Github.
Zend Developer Zone has an excellent tutorial on writing PHP extensions. I wrote this tutorial because the DevZone article is getting a little old: it doesn’t cover objects, methods, or exceptions and it uses Zend API artifacts dating back to PHP 3. However, it is still an excellent tutorial and I highly recommend reading through it if you’re interested in writing PHP extensions.
Setting Up PHP
Before you start developing an extension, you should compile PHP from source (it’ll make debugging easier later on). If you hate future-you, though, you can just do which phpize
and if it returns something, you’re can continue to the next section.
Compiling PHP yourself isn’t too scary (unless your on Windows, in which case welcome to Hell). First, download the source for version you want to develop with. The current stable release is a good choice.
Unpack the tarball and change to the PHP source directory:
$ tar jxvf php-5.3.6.tar.bz2 $ cd php-5.3.6/ $ PHPDIR=`pwd` # setting this up so I can refer to $PHPDIR later
Note: this tutorial assumes that you’re using version 5.3.*. The API changes every version, so if you’re not using 5.3, this tutorial is going to be very frustrating.
To install PHP, run:
$ mkdir install-debug-zts # install dir $ ./configure --enable-debug --enable-maintainer-zts --prefix=$PHPDIR/install-debug-zts $ make install
I recommend using a custom install prefix ($PHPDIR/install-debug-zts in the example above), to keep it separate from any PHP you might have installed previously.
If you install multiple versions of PHP to the default location (/usr/local), it’ll get really annoying really fast: you always have to re-install if you want to try a different build, package managers often install PHP in /usr (so you may have two PHP installs floating around), and the PHP install is oddly coy about overwriting existing files: sometimes it decides to just leave the old versions there if a file already exists.
Thus, it pays to keep things organized in custom installation folders.
There are a couple of configuration options that you should enable, too, for extension development: –enable-debug (debugging info) and –enable-maintainer-zts (thread stuff and memory tracking).
Once make install
is done, you’ve got PHP installed! Add $PHPDIR/install-debug-zts/bin to your path with:
$ # this will only add it to the path for this shell $ PATH=$PHPDIR/install-debug-zts/bin:$PATH
Now you’re ready to make an extension.
Next up: writing your first extension.
cool post!
LikeLike
I transform your post to chinese,Could I? in here :http://jamlee.cn/2015/02/24/php%E6%89%A9%E5%B1%95%E5%BC%80%E5%8F%91%E4%B9%8B%E8%B7%AF-%E4%B8%80/
LikeLike
Please include a link to my original article if you translate it.
LikeLike
of course I do it. Thank you,your post is great and helpful for me.
LikeLike
Very clear and helpful for beginners. Great job you did here! Great beginning php tutorials Great post! Thanks for you insight and sharing it with us! I like this blog and I will be here again.
LikeLike