Advertisment

Add Spice to Your PHP Programs

author-image
PCQ Bureau
New Update

If you develop data driven websites then there are certain things you have to

repeat. For instance, for each and every transaction with the database, you have

to create a connection, log on to it, perform certain transactions and then

disconnect. This not only consumes a lot of your precious time, but also

efforts. You can automate this task by creating custom extensions in PHP.

Advertisment

Extensions can be considered as plug-ins for the PHP engine or simply as an

add-on. They are written mostly in C language and due to this, you can write

processor intensive functions in C and they will work faster than functions that

have been written in PHP. Other than this, suppose you have a code that is

required frequently, you can include this piece of code as an extension rather

than calling it as a function. This makes it faster and more efficient. You can

also use extension in scenarios when you don't want to provide your source code.

Just make an extension and give it away to the devel- oper, but ofcourse with

the necessary documentation.

Direct Hit!
Applies To:

PHP developers



USP:
Create custom extensions for ease of usage



Primary Link:
www.zend.com



Keywords:
NA

Prerequisites



PHP has three kinds of extensions: ZEND engine, built-in and external. In

this article we will create a 'built-in' extension. This is compiled with PHP

and gets loaded along with other PHP processes. Before you can start developing

custom extensions, you need to load PHP and Apache on your workstation. We built

them on a Fedora 7 Linux machine, so the command to install them is as follows:

Advertisment

# yum -y install httpd php mysql mysql-server php-mysql

This command not only installs PHP and Apache, but it also installs MySQL

Server and further integrates PHP and MySQL. However, remember that in order to

run these commands, an Internet connection is a must, because these commands

have to download certain required files.

Custom built module, which

gets loaded with PHP can be viewed under the 'Additional Modules' section
Advertisment

Building a Custom Extension



Let's begin by adding a simple 'hello' extension to PHP. For this you have

to make three different files:



1. First is the source file where the source code will reside,


2. Second is the configuration file which will be required by 'phpize' for
building up the extension. 'phpize' is basically a shell script, which is

required for preparing PHP extensions for compilation.



3. Third is the header file which has references that will be required by PHP
for loading the extensions. In order to avoid any mess-up, let us give the test

directory a name 'hello'.



Now, we will write a simple source code, which will contain the description for
the function 'hello_world()'. Name the file created as 'hello.c'. The source

code for the same is written below:



#ifdef HAVE_CONFIG_H



#include "config.h"


#endif

Advertisment

#include "php.h"



#include "php_hello.h"

static function_entry hello_functions<> = {



PHP_FE(hello_world, NULL)


{NULL, NULL, NULL}


};

zend_module_entry hello_module_entry = {



#if ZEND_MODULE_API_NO >= 20010901


STANDARD_MODULE_HEADER,


#endif


PHP_HELLO_WORLD_EXTNAME,


hello_functions,


NULL,


NULL,


NULL,


NULL,


NULL,


#if ZEND_MODULE_API_NO >= 20010901


PHP_HELLO_WORLD_VERSION,


#endif


STANDARD_MODULE_PROPERTIES


};













Advertisment

#ifdef COMPILE_DL_HELLO



ZEND_GET_MODULE(hello)


#endif

PHP_FUNCTION(hello_world)



{


RETURN_STRING("Hello World", 1);


}

Similarly, create the header file, and name it 'php_hello.h'. The source code

required for creating the header is



written below.

Advertisment

#ifndef PHP_HELLO_H



#define PHP_HELLO_H 1

#define PHP_HELLO_WORLD_VERSION "1.0"



#define PHP_HELLO_WORLD_EXTNAME "hello"


PHP_FUNCTION(hello_world);


extern zend_module_entry hello_module_entry;


#define phpext_hello_ptr &hello_module_entry


#endif

Advertisment

Now, create the configuration file, and name it config.m4. The creation of

the configuration file includes the



following code:


PHP_ARG_ENABLE(hello, whether to enable Hello World
support,



< --enable-hello Enable Hello World support>)

if test "$PHP_HELLO" = "yes"; then



AC_DEFINE(HAVE_HELLO, 1, )


PHP_NEW_EXTENSION(hello, hello.c, $ext_shared)


fi



Finally, to build an extension run the following commands:

# phpize



# ./configure --enable-hello


# make




Once you are through with all these steps, a 'hello.so' file will be generated

in the module directory inside the 'hello' folder. To check whether the
extension you created gets loaded along with PHP, open Internet Explorer, load a
test Web page showing 'phpinfo(),' and look into additional modules. It will

display the extension name 'hello' there. You can also use 'extended_ loaded()'

function to view the loaded extension.

Use 'extension_loaded()'

function to view more details of the extension that you load. The details

would be similar to what is shown in the screenshot above
Advertisment