2. Generating a PHP Extension Skeleton
Writing a basic PHP extension is not too difficult. You only need to create a few files. You can do this manually, but you may also use the “ext_skel.php” script:
$ php php-src/ext/ext_skel.php --ext test --dir .
Unfortunately, this script is not distributed with binary PHP builds and is only available in source distribution. This will create directory “test” with extension skeleton files. Let’s look inside:
$ cd test$ ls
config.m4 config.w32 php_test.h test.c tests
In the above code snippet:
- config.m4 is an extension configuration script used by “phpize” or “buildconf” to add extension configuration options into the “configure” command.
- config.w32 is a similar configuration file for the Windows build system, which is discussed later in this blog.
- php_test.h is a C header file that contains our common extension definitions. It’s not necessary for simple extensions with a single-source C file, but it’s useful in case the implementation is spread among few files.
- test.c is the main extension implementation source. It defines all the structures that allow to plug the extension into PHP and make all their internal functions, classes and constants to be available.
- tests refers to the directory with PHP tests. We will review them later.