Home arrow C++ arrow Page 3 - Developing Custom PHP Extensions: Part 1
C++

Developing Custom PHP Extensions: Part 1


Ever wanted to add your own custom functionality to PHP? In this series of articles Igal will teach us how to do just that using the Microsoft Visual C++ compiler in Windows.

Author Info:
By: Igal Raizman
Rating: 5 stars5 stars5 stars5 stars5 stars / 59
July 18, 2002
TABLE OF CONTENTS:
  1. · Developing Custom PHP Extensions: Part 1
  2. · What is a PHP extension?
  3. · Setting up our Development environment
  4. · Our First Extension
  5. · Conclusion

print this article
SEARCH DEVARTICLES

Developing Custom PHP Extensions: Part 1 - Setting up our Development environment
(Page 3 of 5 )

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:\devarticles\php-dev\php-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:\devarticles\php-dev\php-4.2.1\ext\ 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.

Now, the following few steps must, unfortunately, be completed for each extension project you create:

1. Under the Build menu select Configuration (Build->Configuration). In that dialog, add two configurations: Release_TS and Debug_TS. Make sure Release_TS copies the settings from the Release configuration and Debug_TS copies the setting from the Debug configuration. After this is done, remove the Release and Debug configurations. Make sure the dialog looks as follows and close it:



2. Now we will set the options in the Release_TS configuration. Open up Project > Settings. On the C/C++ tab, General Category, add the following preprocessor definitions: ZEND_DEBUG=0, COMPILE_DL_YOUR_EXTENSION_NAME, ZTS=1, ZEND_WIN32, PHP_WIN32. YOUR_EXTENSION_NAME is the name of your extension. For example: COMPILE_DL_DEVARTICLES_MOD.



3. In the Code Generation Category, change Use run-time Library to "Multithreaded DLL".

4. In the Preprocessor Category, add the following "Additional include directories": ..\.., ..\..\main, ..\..\Zend, ..\..\TSRM, ..\..\bindlib_w32.

The directories are relative to where your project is found. Since our project lives in the \ext directory, the paths are right. However, if you plan to hold your extensions elsewhere you will need to change the directories appropriately.



5. On the Link tab in the General Category, change the Output file name to ..\..\Release_TS/php_devarticlesmod.dll . In future projects you will be able to change the name to anything (*.dll). However, most PHP extensions come with the prefix php_ to indicate they are in-fact PHP extensions. (php_*.dll)

6. On the same tab (Link), same category (General), add php4ts.lib to Object/library modules.

7. In the Input Category, add the following to Additional library path: ..\..\Release_TS.

Close the dialog and set the active configuration to Release_TS. This is done by the Build->Set Active Configuration... dialog.

Our development environment is almost set or it is completely set, depending on what you do next. You see, PHP was written in C and not C++, thus all extensions must be written in such a way that they use the C naming convention. Using C++ and not C would cause a linker error (LNK2001 to be exact).

When you add a new source code file to your project, the default extension (not to be mixed with our topic) is *.CPP. This extension causes the compiler to use the C++ naming convention and ultimately leads to the linker error. You can avoid this by changing the extension on the source code file to .C, which will force to compiler to use the C naming convention. On the other hand, if you prefer to continue using .CPP files you will need to add the following option in your project options: /Tc

This will force to compiler to use C naming convention even though it is a C++ source code file.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials