Saturday, April 3, 2010

Creating a simple php extension

(This article assumes that you have installed apache2,php and all the other development tools in your machine. To do those things there's an article with all the instructions)

To make a php extension we can use the mechanism provided in the php source code which is the shell script named ext_skel. First download the latest php source code and find the ext_skel shell script in the ext directory in source code.

1. Give this command from the terminal in ext directory.

./ext_skel --extname=TikiriDB

Now a directory will be created in ext called TikiriDB. Then we can use that extension skeliton to build our module.

2. Go to the TikiriDB directory. Open the file TikiriDB.c file and add the line PHP_FE(tikiri_print, NULL) in appropriate location to see like,

const zend_function_entry TikiriDB_functions[] = {

PHP_FE(confirm_TikiriDB_compiled, NULL)

PHP_FE(tikiri_print, NULL)

{NULL, NULL, NULL} /* Must be the last line in TikiriDB_functions[] */

};

There's another thing which should be added to the same file. Add the code below at the end of the file.

PHP_FUNCTION(tikiri_print){

char *text = NULL;

/* count the number of arguments passed */

int argc = ZEND_NUM_ARGS();

int text_len;

/* TSRMLS_CC boils down to ", tsrm_ls" where tsrm_ls is a resource handler.

You don't need to worry about this, it is used to make it thread safe */

if (zend_parse_parameters(argc TSRMLS_CC, "s", &text, &text_len) == FAILURE) {

php_error_doc_ref(NULL TSRMLS_CC, E_ERROR, "Invalid Parameters");

return;

} /* Copy the value from the passed string, create a new one and return it */

RETURN_STRING(text, 1);

}

3. Now open the file php_TikiriDB.h. Add the line PHP_FUNCTION(tikiri_print); to the appropriate location to be like this.

PHP_MINIT_FUNCTION(TikiriDB);
PHP_MSHUTDOWN_FUNCTION(TikiriDB);
PHP_RINIT_FUNCTION(TikiriDB);
PHP_RSHUTDOWN_FUNCTION(TikiriDB);
PHP_MINFO_FUNCTION(TikiriDB);
PHP_FUNCTION(confirm_TikiriDB_compiled);
PHP_FUNCTION(tikiri_print);

4. Open the file config.m4 and remove the comments remain the following part uncommented.

PHP_ARG_ENABLE(TikiriDB, whether to enable TikiriDB support,

[ --enable-TikiriDB Enable TikiriDB support])

5. Now give this command inside the TikiriDB directory,

phpize

./configure --enable-TikiriDB

make

6. Now in the TikiriDB directory open the file Makefile and in the EXTENSION_DIR attribute give the path to the extension directory of your installed php.

eg- If your extensions are in the directory /usr/local/include/php/ext set the path as,

EXTENSION_DIR = /usr/local/include/php/ext

7. Now give this command inside the TikiriDB directory,

make install

8. The extension is now installed. Open your php.ini file and make sure the extension_dir attribute is set to the same path.

Add an entry to it as,

extension=TikiriDB.so

9. Now in your apache servers htdocs directory put a file named testing.php and put the content below in that folder.

$string = tikiri_print("Hello World");
echo $string;

10. Start your apache server and access the file testing.php from the web browser. The function tikiri_print returns the string given to it.

If you want to change the functions defined in the module several time and test again and again, you have to repeat the steps,


/usr/local/bin/phpize
./configure --enable-TikiriDB
make


Copy the new TikiriDB.so file in the modules folder of the TikiriDB folder to the extension directory of the installed php's extension directory overwriting the old TikiriDB.so file.

1 comment:

  1. Very useful guide.
    I don't think your effort is wasted.

    ReplyDelete