Welcome, in this tutorial, i will explain how to write your own php extension.
I will explain in easy to understand steps, how you could write your own php extension.
This tutorial is set for intermediate C++ users.
Introduction
Quote
What is php, and how would i create a php extension?
Well, php is a widely used all purpose scripting language that is specifically suited for Web development. Some php knowledge is required for this tutorial, so if your new and do not wish to tackle this tutorial first up, the why not check some php tutorials out at these web sites.
In this tutorial we will be using the zend.h file.
What is Zend: The Zend Engine refers to Php's core. The Zend Engine is the heart of php. So without the Zend Engine, there would be no php
What is Php: Php stands for Php:HypertextPreProcessor. This refers to the COMPLETE Php system.
What You Will Need
In this tutorial, a few things are needed to follow it on through.
First up, for the purpose of this tutorial, i am using Microsoft Visual C++ Express 2008. Witch can be downloaded Here for Free. Though the time consumption downloading it is long, its worth the wait.
The second thing you will need, is a working local server. I specifically use wamp server, witch can be downloaded Here for Free. I will explain how to setup wamp server and the server side to things later.
You will also need to download the Php 5.2.5 Version for wamp sever witch can be downloaded from the source forge web site Here
Also, another thing is, the actual php 5.2.14(Witch will suit php 5.2.5) source files that can be downloaded Here @ Php.net.
Ok, now on to setup and configure our server and source codes.
When you have downloaded the 3 Server files
- Wamp Server
- Wamp Server Php 5.2.5 Version
- Php 5.2.14 Source files
You should have something that looks like this: Fig 01.
Installing Wamp Server
Please follow these instructions on how to install Wamp Server
- Run WampServer.exe - A screen will pop up, click next like fig 02
- A new screen will appear, click i accept if you accept else click i do not accept to agreement, then click next like fig 03
- Then, when you are at the Install Directory part, just click Next, to let it install it to its usual directory, see fig 04
- Next, when it asks you if you want a desktop shortcut ect, click both check boxes as shown in
fig 05 - Bearing the end, you will come to a screen with the install information, click "Install" to install wamp, as shown in fig 06
- Once it has installed, you will be prompt with a message about your browser, just click "Ok/Yes".
- Then, another screen will show specifying stuff about your, mail server and all, just click ok for defaults, as shown in fig 07
- Finally, you have come to the last screen, click Ok to launch Wamp Server, as shown in fig 08
Ok, by now, you should of installed either Microsoft Visual C++ Express 2008 or your own IDE choice and a working local server.
Installing PHP 5.2.5 for wamp
This process is very much similar to the one above, except it only has 4 windows, just keep clicking next until it has done.
When the process is complete, it will show a screen like fig 07, just click next, then when it says "Launch Wamp Server" Click next.
Was that easy enough? Hope so, because the following Installation step, might be a bit difficult for you.
Installing the Php 5.2.14 Source Codes
Ok, in this installation step, i will tell you how to install the Php 5.2.14 source codes.
Ok lets get started.
- Ok, Now, when you have downloaded the source files, it should be in a zip file.
- Extract them to the following directory: "C:\wamp\bin".
- Once done, there should be a file called "php-5.2.14" with a lot of sub folders. This means you have installed the source codes correctly
- That's it, your done
Now you are ready to make your own Php Extension in C++
Creating a Php Extension in C++
In this part of the tutorial we will
- Creating Our Project
- Setup Microsoft Visual C++ Express to our needs
- Create our Php Extension
- Make our Php Accessible in php
- Testing Our Php Extension
Creating Our Project
In this sector, we will create and name our project.
In Microsoft Visual C++ Express 2008, Go to the file tab and click New->Project
Next, a screen will show up called "New Project".
In the "Project Types Window(The one to the left) click the little arrow in front of Visual C++.
This will expand a menu with the following contents.
- CLR
- Win32
- General
Next, click the "General" tab. To the right, in the "Templates" Window, click "Win32 Project".
We are hiking through this tutorial
Now, below, you will see some fields.
Enter a name into the name field, for the purpose of this tutorial, we will use "tutorial".
Click ok, this will bring up a screen called "
Win32 Application Wizard - Tutorial", click next.
By now you should see a window with "Application Settings". Stay on the page and under "Application Type" click "DLL", then click finish.
Now, following the finish event, a window should open in the text editor called "Tutorial.cpp", which should contain this piece of code:
// tutorial.cpp : Defines the exported functions for the DLL application. // #include "stdafx.h"
Now, to set up our IDE "Microsoft Visual C++ Express 2008"
Setting Up Microsoft Visual C++ Express 2008 To Our Needs
Ok, there is a little bit of stuff to take care of before we create our php extension, the following will allow us to compile our extensions.
This will tell the compiler, where to find all of the zend functions.
Ok, lets get moving, shall we!
Ok, to start, navigate to the "Solution Explorer"(To The Left).
Right Click on your project, and navigate to "Properties".
Once "Properties" has been clicked, a window will open called "tutorial - Properties Page".
Navigate to "Configuration Properties -> C/C++". In the window to the right, you should see 8 fields.
The first field should be called "Additional Include Directories" click on the field, and a button to the right with a few dots "..." should pop up. Click it.
Next, another window should open called "Additional Include Directories. to the right, there are a bunch of buttons. You should see a button with a folder, click it or press "ctr->insert".
This should automatically add an entry to the box. Click on the entry and click the browse button "...".
Navigate to the following directory:
"C:\wamp\bin\php-5.2.14" - Where you extracted the php 5.2.14 source code to.
Then repeat the
Quote
You should see a button with a folder, click it or press "ctr->insert".
This should automatically add an entry to the box. Click on the entry and click the browse button "...".
Navigate to the following directory:
This should automatically add an entry to the box. Click on the entry and click the browse button "...".
Navigate to the following directory:
- C:\wamp\bin\php-5.2.14
- C:\wamp\bin\php-5.2.14\TSRM
- C:\wamp\bin\php-5.2.14\Zend
- C:\wamp\bin\php-5.2.14\Regex
- C:\wamp\bin\php-5.2.14\Main
Then click "Ok".
This should bring you back to the "Configuration Properties -> C/C++".
Now, to set up some processor stuff.
We need to navigate to "Configuration Properties -> C/C++ -> Preprocessor". To the right you should see a field called "Preprocessor Definitions". Click the Browse button "..." button, this will open a window called "Preprocessor Definitions", do you see this line "TUTORIAL_EXPORTS"? add this directly beneath it.
PHP_WIN32 ZEND_WIN32 ZTS=1 ZEND_DEBUG=0
Now it should look like this:
WIN32 _DEBUG _WINDOWS _USRDLL TUTORIAL_EXPORTS PHP_WIN32 ZEND_WIN32 ZTS=1 ZEND_DEBUG=0
Press ok and you should come back to the "Configuration Properties -> C/C++ -> Preprocessor" area.
Hard work
Its all worth it though.
Now, Navigate to "Linker -> General" and find the field called "Additional Library Directories". Add the following directory to the field "C:\wamp\bin\php\php-5.2.5\dev".
Next, Navigate to "Configuration Properties -> Linker -> Input", do you see a field called "Additional Dependencies", add this to the field "php5ts.lib"
Now, Navigate to "Configuration Properties -> Linker -> Command Line" and in the box on the bottom add this to the field "/FORCE:MULTIPLE"
One last thing
Now, Navigate to "Configuration Properties -> Linker -> General" and add the following to the output field "$(OutDir)\tutorial.dll"
Now click apply, then ok. This is it for this part of the tutorial, next, we will write our php extension.
Creating a Php Extension in C++
Ok, now we get to the fun part of this tutorial
First up, i will display the full code of our php extension.
The extension will make a function called "Tutorial" and it will be called in php by using the following function(s):
tutorial();
or
tutorial();
#include "stdafx.h"
ZEND_FUNCTION(tutorial);
zend_function_entry tutorial_functions[] = {
ZEND_FE(tutorial, NULL)
{NULL, NULL, NULL}
};
zend_module_entry tutorial_module_entry = {
STANDARD_MODULE_HEADER,
"Tutorial Info",
tutorial_functions,
NULL, NULL, NULL, NULL, NULL,
"1.0 - Alpha",
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(tutorial);
ZEND_FUNCTION(tutorial)
{
bool useHtml = false;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &useHtml) == FAILURE)
{
RETURN_STRING("Missing Parameter", true);
}
if (useHtml == true)
{
php_printf("<a href=\"http://studios.thompsonhosting.com\">Thompson Gaming Studios</a>");
}
else
{
php_printf("http://studios.thompsonhosting.com");
}
}
if you know C++, you probably understand most of it.
Now, see the
#include "stdafx.h"
Hover your mouse on over it and right click, click "Open Document "stdafx.h".
Now, replace the contents with this code:
//Php - Lite extension header file #pragma once //Includes the zend_config.w32 header file and the php header file #include "zend_config.w32.h" #include "php.h"
Ok, we have to preform a little modification to the "zend.config.h" header file.
Right click, and click "Open Document "zend.config.h"".
Now scroll down to round about line "51" until you see a line that says "#define vsnprintf _vsnprintf".
Have you found it? Comment it our or remove it, this will have no affect on your extension.
Then,go to "Build -> Build Solution".
Make our Php Accessible in php
To make our php extension in php, we need to edit the "php.ini file.
First up, Open "Php.ini", scroll down to the extension part and add "extension=tutorial.dll"
Save the file and then restart all Wamp Servers Services.
And that's it, all that to enable our php extension
Next, we will test our php extension
Testing our own Php Extension
Ok, here, we will test our php extension we have just made.
Open the IDE of your choice that is able to open and edit php file. I use Adobe Dreamweaver CS5.
Create a new page called "test_extension.php". Add the following code to it.
<?php echo "This link is using Html: ".tutorial(true)."<br />"; echo "This link is not using html: ".tutorial(); ?>
That will produce
Quote
This link is using Html: Thompson Gaming Studios
This link is not using Html: http://studios.thompsonhosting.com
This link is not using Html: http://studios.thompsonhosting.com
Save the file, and then go to your browser and type in the address "http://localhost/test_extension.php"
If the above worked, then you have a full functional php extension.
Conclusion
By the end of this tutorial, you should know how to create a Php Extension, add a function to the extension and making it work in php.
I hope this was easy to read, i hope you have gained something out of it
Thanks for reading
Thanks Aaron1178






MultiQuote




|