Been reading Jacob's blog for a while now, really interested in the gaming library work going on and one of the things that came up was making an extension to accelerator the gaming library.
Its been a while since I last played around with extensions, not since php4 so I scanned through the manual and some only tutorials and threw together this quick and dirty guide to help you get started, again quick and dirty, not a hand holding ends all guide to making an extension, if you don't know some C and php already this is not for you.
I was gratefull to find out that php5 still has a nifty little utility called ext_skel which does a large part of the work for you, making the best use of this utility just requires a little planning ahead of time.
So, gaming library, what type of functions would I need for a gaming library? Distance is important lets do the following
double sq_grid_distance(x1, y1, x2, y2) Calculate square grid distance
Ok so the above is important , you should save this in a file called gaming.proto. This prototype file will tell the ext_skel utility what functions you plan on creating and allow it to build a skeleton for you.
So now that you have a prototype file its pretty simple to get started lets go to the php source php/ext and run the following ./ext_skel --proto=/path/gaming.proto --extname=gaming
Now enter the new directory that was just created, gaming and you will find everything you need to get started.
First thing you need to worry about is the config.m4, This is pretty much full of comments, we need just a few lines of code from here to get the results we want, first off
Make sure that the comment is aligned:
[ --enable-gaming Enable gaming support])
This little bit tells php config that your extension can be enabled by using ./configure --enable-gaming, The rest of the code is
PHP_NEW_EXTENSION(gaming, gaming.c, $ext_shared)
fi
This little bit of code registers the new extension, You can put checks prior to PHP_NEW_EXTENSION to check for any possible libraries you might need to compile, if you don't fine those libraries you would error out never registering the extension to be compiled.
Now you can open up gaming.c, read through this a bit as there are a lot of great notes in here that are usefull. First thing we notice is a note and some function called confirm_gaming_compiled, this is a test function that would allow you at this time to compile this extension into php and it would output a quick note telling you your config.m4 is working, we can remove this this line
And while you are at it bounce down and remove the function from the code itself, right under that you will find the distance function, we expect 2 args so lets change that and also get rid of the php_error saying this function is not yet implemented because we are going to do that right now.
WRONG_PARAM_COUNT;
}
php_error(E_WARNING, "distance: not yet implemented");
To
WRONG_PARAM_COUNT;
}
Now we need to get our input
return;
}
The "ll" bit tells us we have 2 inputs, both long integers
Now we finish this up with
Your final function looks something like this
You should now be able to compile this into php using --enable-gaming, first you need to rebuild configure so it knows about it by running ./buildconf in the base php path, for testing though I prefer to just do the following in the module directory to compile it as a seperate module I can include and test without having to recompile all of php
./configure
make
make install
You now have a gaming.so you can load and call the new distance function, using the above example it should be pretty easy to do sq_grid_distance yourself.
One thing to know, there are a lot of special internal functions like RETVAL_DOUBLE that zend uses to handle and protect variables and memory, if you get beyond simple math its very likely you will need to use these otherwise your extension will crash php, otherwise enjoy ;)

