Developing Custom PHP Extensions
The latest news and info on web development from DevNewz
09.09.02
Search iEntry News:
Developing Custom PHP Extensions

What is a PHP extension?

A PHP extension, in the most basic of terms, is a set of instructions (i.e. code) that is designed to add functionality to PHP. For example, the widely used GD library (used for the creation of dynamic images) is an extension. This library added new functionality by allowing PHP to generate images on the fly. Another example is the MySQL extension, which allows us to connect to and work with MySQL databases.


What are PHP extensions needed for? There are several reasons why extensions are needed. One of them, as stated above, is to add new functionality to PHP. For instance, where would we be today if someone did not add the functionality to work with MySQL? Where will we be tomorrow if someone does not add the functionality to work with tomorrow’s databases or tomorrow's technologies? As PHP continues to grow, it is likely that new "features" will be required by the ever-growing number of web developers. Some of the new features will be popular enough to be added to the official distribution, while others will not. Either way, those extensions will serve their creators well.

On the other hand, we might use PHP extensions to improve the efficiency and speed of our programs. Some processor intensive functions might be better coded as an extension rather than straight PHP code. Since extensions are written in C (more on the actual coding later), they will work much faster than straight PHP code too.

Another possible reason to employ extensions is to reuse frequently utilized code. Instead of moving the same old functions from project to project, you could place them all in one extension and allow all your projects to utilize that extension.


FREE WEBINAR Make your Enterprise Portal Work the Way People Think! Empower your portal users to get work done more effectively and efficiently.

How do I develop my own extensions?

Before this question can be answered, we must look at the different "types" of extensions available. Extensions come in three different flavors: Zend engine extensions, built-in extensions and external extensions.

Zend Engine extensions are extensions that are implemented right into the engine itself. For those of you who do not know, the Zend engine is what PHP is built on. It is the engine that parses, interprets and executes your PHP scripts. Changing the engine itself will change the way PHP works. Anything that will affect the language itself or its features is added to the Zend engine; this includes if statement evaluation, object orientation, mathematical expressions evaluation, etc.

Although extending the engine is possible, it's not recommenced for reasons such as incompatibility with servers that run the officially distributed engine. In other words, not too many server administrators will agree to use an unofficial version of the Zend engine.

Built-in extensions are extensions that are compiled right into PHP and are loaded with the PHP processes. The advantages of this method are: programmers aren't required to load extensions manually, and no extension files are required (since it is compiled right into the PHP binary itself). The disadvantages, on the other hand, are: any changes to the extension will require a complete re-compilation of the PHP binary itself, and the size of the binary will grow with each new extension (as will the amount of memory it will consume).

FREE DOWNLOAD! Hands-off Management Tool. Easy-to-use systems management solution that helps reduce the total cost of ownership for desktops, notebooks, and handheld devices.

External extensions are extensions that are manually added during run time. All the functionality of the extension will be available to the script that loaded it. When the script ends, the extension is released and the memory is freed. As you might guess, the advantages are: only the extension itself needs to be re-compiled after any changes and a small PHP binary. Also, you don’t provide the functionality of your extension to scripts that do not require it. And, as always, where advantages go, disadvantages follow: extensions are loaded during run time, a process that takes time, and the programmer must remember to load the extension since it is not automatically available.

Although loading external extensions each time the script is executed takes time, it is fairly quick. I personally do not feel any speed differences when I load my external extensions. Of course, if the site receives heavy traffic, a speed difference might be apparent and built-in extensions might be the most appropriate solution. Nevertheless, in these articles we will develop an external extension. Note that the difference between built-in extensions and external extensions – code wise – is virtually nonexistent.


FREE WHITE PAPER Keep Your Knowledge Data Base From Becoming Obsolete - Advanced search capabilities that provide keyword, natural language searches, and content relevancy ranking.

Setting up our Development environment

In this tutorial I will use Microsoft's Visual C++ 6.0 compiler. Compiling extensions on Unix / Linux systems is fairly well documented. On windows, however, this isn't the case.

The first thing you must do is download the PHP source code distribution, php-4.2.1.tar.gz, from the PHP web site.

At the time of writing the latest version of PHP was 4.2.1

Once the source code is downloaded, create a directory where you will keep the source code and where you will hold the sources of your extensions. In my case, this will be D:devarticlesphp-devphp-4.2.1. Extract the contents of php-4.2.1.tar.gz into that directory (I used WinRAR to extract the file).

Next, move into the ext directory, which is D:devarticlesphp-devphp-4.2.1ext in my case. There you will see all of the different extensions that are currently being distributed with PHP. Any time during your coding process when something does not work, it is a good idea to just open an extension and read through the source code.

Moving right along, open your MS Visual Studio and make sure no other projects or files are open. Under the File menu, choose New and from the list of wizards choose the "Win32 Dynamic-Link Library". All PHP extensions under windows must be in form of a DLL, thus it is imperative to choose the right type of project. Choosing anything different will result in ugly and indecipherable linker errors (If you've ever worked with Visual C++, no doubt you are familiar with the cryptic messages the compiler throws at you).

Now, for the location. Enter – or rather browse to – the extension subdirectory in your PHP development directory. For the project name enter "devarticles" and this should be appended automatically to your location.

Click the OK button. On the next menu choose "An empty DLL project" and click the finish button. A new window should open up and show you all the information relating to the project; click the OK button again. Click here to continue the article.



Resources


WebmasterFree
Download
For more free downloads from WebmasterFree - Click Here


Free Newsletters

Web Developers / Programmers
DesignNewz
DevNewz
DevWebPro.
DevWebProAU
DevWebProCA
DevWebProUK
FlashNewz
JavaProNews
MacProNews
TheDevWeb
ThePerlJournal
UnixProNews
WebProASP
XMLProNews

IT Managers/ Decision Makers
DatabaseProNews
EnterpriseEcommerce
HiTechEdge
ITcertificationNews
ITmanagementNews
LinuxProNews
NetworkNewz
SecurityProNews
SysAdminNews
WinXPdigest

eBusiness Management
CareerNewz
CRMNewz
EcommNewz
InvestNewz
ManagerNewz
MarketingNewz
NewsletterIndustry
SalesNewz
WebProNews

IT Software
FLADownloads
FreewareToday
LinuxWebmasterFree
MacWebmasterFree
WebmasterFree



-- DevNewz is an iEntry, Inc. ® publication --
© 2002 iEntry, Inc. All Rights Reserved Privacy Policy and Legal
archives | advertising info | news headlines | free newsletters | comments/feedback

DevNewz for professional developers visit iEntry.com for more free newsletters