PHP is a very powerful programming language, but there are some situations where it can limit what you can do both in terms of performance and  lack of a specific functionality that maybe is already available as low level library.

In these cases write a specific extension that fits your needs can be a good solution.

To write your first “Hello World” extension you need a compiler (gcc is a good one) and PHP with developer tools (phpize, php-config).

To obtain php developers tools:

On CentOS/Fedora: yum install php-devel;

On Debian/Ubuntu: apt-get install php5-dev;

Our “Hello World” extension, to work needs at least three files.

The first is the config.m4 this file is used when issuing a phpize to tell the compiler how to build your extension.

dnl config.m4 for extension hello_world

PHP_ARG_WITH(hello_world, whether to enable hello world support,
[  --enable-hello-world       Enable hello world])

if test "$PHP_HELLOWORLD" != "no"; then
  PHP_NEW_EXTENSION(hello_world, hello_world.c, $ext_shared)
fi

The second is the php_hello_world.h file that will contain all extension’s definitions and include all needed libraries.

#ifndef PHP_HELLO_WORLD_H
#define PHP_HELLO_WORLD_H

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"

#define PHP_HELLO_WORLD_NAME "hello_world" /* Replace with name of your extension */
#define PHP_HELLO_WORLD_VERSION "0.1.0" /* Replace with version number for your extension */

#endif	/* PHP_HELLO_WORLD_H */

The last one needed file is the hello_world.c that will contain all the source code of our extension

#include "php_hello_world.h"

PHP_FUNCTION (hello_world) {
	char *arg = NULL;
	size_t arg_len, len;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
		return;
	}

	char greet[1024];
	strcpy(greet, "Hi ");
	strcat(greet, arg);
	strcat(greet, "!");

	RETURN_STRING(greet, 1);
}

PHP_MINIT_FUNCTION (hello_world) {
	return SUCCESS;
}

const zend_function_entry hello_world_functions[] = {
PHP_FE(hello_world, NULL)
PHP_FE_END };

zend_module_entry hello_world_module_entry = {
STANDARD_MODULE_HEADER,
PHP_HELLO_WORLD_NAME,
hello_world_functions,
PHP_MINIT(hello_world),
NULL,
NULL,
NULL,
NULL,
PHP_HELLO_WORLD_VERSION,
STANDARD_MODULE_PROPERTIES };

#ifdef COMPILE_DL_HELLO_WORLD
ZEND_GET_MODULE(hello_world)
#endif

Now it’s time to put the pieces togheter and build the extension issuing these commands:

phpize
./configure
make

Now your extension is compiled but not yet installed on the system so you can test if it works with:

php -d extension=modules/hello_world.so -r "echo hello_world("Lorenzo");"
That will output:

Hi Lorenzo!

And when you are ready to install the extension issue a:

make install

After running the make install you will have a hello_world.so in your php extensions directory, to enable hello_world as php extension just append this yo your php.ini

[hello_world]
extension=hello_world.so

Now to test your extension type the following command:

php -r 'echo hello_world("Lorenzo");'

As before the output should be something like:

Hi Lorenzo!

Comments

comments powered by Disqus

cloudparty

Follow Us