Article continued
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.
Our First
Extension
If you followed all of the instructions
carefully then you should be able to commence writing your own
extensions. I will leave all the serious coding to the next
article and just show you a basic extension that prints "Hello
World" five times. Copy and paste the following code into your
project; compile and build it:
/*
include standard header */ /* you will need to include this
in all of your php extension projects*/ #include "php.h"
/* All the functions that will be exported (available) must
be declared */ ZEND_FUNCTION(hello_world); PHP_MINFO_FUNCTION(devarticlesmod);
/* Just a basic int to be used as a counter*/ int i;
/* function list so that the Zend engine will know what’s here
*/ zend_function_entry devarticlesmod_functions[] = { ZEND_FE(hello_world,
NULL) {NULL, NULL, NULL} };
/* module information */ zend_module_entry devarticlesmod_module_entry
= { STANDARD_MODULE_HEADER, "DevArticles", devarticlesmod_functions,
NULL, NULL, NULL, NULL, PHP_MINFO(devarticlesmod), NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES };
#if COMPILE_DL_DEVARTICLES_MOD ZEND_GET_MODULE(devarticlesmod)
#endif
PHP_MINFO_FUNCTION(devarticlesmod) { php_info_print_table_start();
php_info_print_table_row(2, "DevArticles Extension", "All Systems
Go"); php_info_print_table_end(); }
ZEND_FUNCTION(hello_world) {
for(i=0;i<5;i++) { zend_printf("Hello World
"); } }
Don't worry if you do not understand a lot of what's in the
C code above. This article is only meant to show you how to
setup the environment and explain how to use the extensions
in your scripts. All of the code-related material will be explained
in the subsequent article(s).
After you've compiled and built the extension, you should take
the newly created .dll file (php_devarticlesmod.dll in our case)
and place it in your web-server directory. In that same directory,
create a new PHP file and type the following:
dl("php_devarticlesmod.dll");
hello_world();
phpinfo();?>
The first line tells php to load the module/extension. The function
takes the name of the extension file as the parameter.
The second line, hello_world(), is the function we created in
our extension. Upon calling this function you should see "Hello
World" printed five times on the screen.
And, lastly, phpinfo() is called. If you scroll down through
the output you should see a confirmation that your extension
is indeed loaded:
Conclusion
Although we haven’t created an extension
just yet, we’ve laid the necessary foundation to do so in the
next article. You now have an understanding about extensions
in PHP, the different kinds of extensions that we can create,
and most importantly, you have the development environment set.
In the next article I will assume the development environment
is set, and we will begin working on our extension. Some of
the topics that will be covered are: Passing variables to functions
Returning values from functions Memory allocation and management
Zend engine macros
For more great programming articles, please visit
www.devarticles.com. If you have an opinion or question
about this article, then please post it at the devArticles developer
forum, which is located at www.devarticles.com
|
WebmasterFree
Download
|
|
For
more free downloads from WebmasterFree - Click
Here
|
|