PHP 4 Delphi
PHP 4 Delphi
PHP 4 Delphi
0
PHP Development Framework for Delphi
1. Introduction
PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source
general-purpose scripting language that is especially suited for Web development and can be
embedded into HTML. Its syntax draws upon C, Java, and Perl, and is easy to learn. The main
goal of the language is to allow web developers to write dynamically generated WebPages
quickly, but you can do much more with PHP.
PHP4Delphi 7.0 provides Visual Development Framework for creating custom PHP
Extensions using Delphi. PHP extension, in the most basic of terms, is a set of instructions that is
designed to add functionality to PHP.
PHP4Delphi also allows executing the PHP scripts within the Delphi program directly
from file or memory. You can read and write global PHP variables and set the result value.
PHP4Delphi allows you to embed the PHP interpreter into your Delphi application so
you can extend and customize the application without having to recompile it.
The creation of a PHP Extension DLL is similar to the development of any standard DLL. For
this purpose it is necessary to load Delphi, in the menu
File choose item New, then in a New Items dialog box
choose an icon PHP Extension and to press the OK
button.
PHP4Delphi provides a full design time environment for the development of PHP Extensions. A
special Data Module PHPExtension is added to your new project; You can place any non-visual
controls in it and work with them.
• Functions: Contains all the functions that are to be made available externally, with the
function's name as it should appear in PHP
2.2. TPHPExtension Events
• OnCreate: Occurs when an application instantiates the data module. Write an OnCreate
event handler to take specific actions when an application instantiates the data module.
For example, if the data module contains database and dataset components, an
application may establish a database connection immediately.
• OnDestroy: Occurs when the data module is about to be destroyed. Write an OnDestroy
event handler to take specific actions when an application frees a data module. For
example, if the unit code for the data module instantiates any objects of its own, such as a
TStringlist, the OnDestroy event handler can be used to free those objects.
• OnModuleInfo: When phpinfo() is called in a script, Zend cycles through all loaded
modules and calls this function. Every module then has the chance to print its own
"footprint" into the output page. Generally this is used to dump environmental or
statistical information.
• OnModuleInit: This event occurs once upon module initialization and can be used to do
one-time initialization steps (such as initial memory allocation, etc.).
• OnModuleShutdown: This event occurs once upon module shutdown and can be used
to do one-time deinitialization steps (such as memory deallocation). This is the
counterpart to OnModuleInit event.
• OnRequestInit: This event occurs once upon every page request and can be used to do
one-time initialization steps that are required to process a request. Note: As dynamic
loadable modules are loaded only on page requests, the request startup function is called
right after the module startup function (both initialization events happen at the same
time).
• OnRequestShutdown: This event occurs once after every page request and works as
counterpart to OnRequestInit event. Note: As dynamic loadable modules are loaded
only on page requests, the request shutdown function is immediately followed by a call to
the module shutdown handler (both deinitialization events happen at the same time).
2.3. Add functions
TPHPFunction properties:
• Name: Denotes the function name as seen in PHP (for example, fopen, mysql_connect,
or, in our example, myfunction).
Next write an OnExecute event handler to implement a response to function call sent by the
PHP script.
Example:
To test your project you can use PHP script like this:
<?
if(!extension_loaded('extname')) {
dl('skeleton.dll');
}
$str = confirm_extname_compiled("skeleton");
echo "$str\n";
$module = 'extname';
if (extension_loaded($module)) {
$str = "module loaded";
} else {
$str = "Module $module is not compiled into PHP";
}
echo "$str\n";
$functions = get_extension_funcs($module);
echo "Functions available in the $module extension:<br>\n";
foreach($functions as $func) {
echo $func."<br>\n";
}
?>
You can also build PHP extensions in the classical way – using the ZEND API.
You can find the ZEND API documentation here: http://www.zend.com/apidoc/
We'll start with the creation of a very simple extension at first. This basically does nothing more
than implement a function that returns a string.
library skeleton;
uses
Windows, SysUtils, zendTypes, ZENDAPI, phpTypes, PHPAPI;
var
moduleEntry : Tzend_module_entry;
module_entry_table : array[0..1] of zend_function_entry;
exports
get_module;
end.
To declare functions that are to be exported (i.e., made available to PHP as new native
functions), you have to add procedures with the following declaration:
procedure <procedure name> (ht : integer; return_value : pzval; this_ptr :
pzval; return_value_used : integer; TSRMLS_DC : pointer); cdecl;
Parameter Description
Ht The number of arguments passed to the Zend
function.
Return_value This variable is used to pass any return values
of your function back to PHP.
This_ptr Using this variable, you can gain access to the
object in which your function is contained, if
it's used within an object.
Return_value_used This flag indicates whether an eventual return
value from this function will actually be used
by the calling script. 0 indicates that the return
value is not used; 1 indicates that the caller
expects a return value. Evaluation of this flag
can be done to verify correct usage of the
function as well as speed optimizations in case
returning a value requires expensive operations
TSRMLS_DC This variable points to global settings of the
Zend engine. You'll find this useful when
creating new variables, for example
Now that you have declared the functions to be exported, you also have to introduce them to
Zend. Introducing the list of functions is done by using an array of zend_function_entry. This
array consecutively contains all functions that are to be made available externally, with the
function's name as it should appear in PHP and its name as defined in the Delphi source.
zend_function_entry = record
fname : Pchar;
handler : pointer;
func_arg_types : Pbyte;
end;
Entry Description
Fname Denotes the function name as seen in PHP (for
example, fopen, mysql_connect, or, in our
example, first_module).
Handler Pointer to the Delphi function responsible for
handling calls to this function
Func_arg_types Allows you to mark certain parameters so that
they're forced to be passed by reference. You
usually should set this to Nil.
You can see that the last entry in the list always has to be {Nil, Nil, Nil}. This marker has to be
set for Zend to know when the end of the list of exported functions is reached.
4. Setup
PHP4Delphi is a Delphi interface to PHP. It works with Delphi 5, 6, 7, Delphi 2005 and Delphi
2006.
PHP4Delphi allows you to execute PHP scripts from within your Delphi program directly,
without needing a Web Server. PHP4Delphi also contains the PHP API and ZEND API and a
visual development framework for PHP extensions.
This is a source-only release of php4Delphi. It includes design time and runtime packages for
Delphi 5 through Delphi 2006.
If you do not have PHP installed, you have to download and install PHP separately. It is not
included in the package. You can download the latest version of PHP from
http://www.php.net/downloads.php
You need to ensure that the dlls which php uses can be found. php4ts.dll (php5ts.dll) is always
used. If you are using any PHP extension dlls then you will need those as well. To make sure that
the dlls can be found, you should copy them to your system directory (e.g. winnt/system32 or
windows/system).
1. Delphi 5.x:
Uninstall any previously installed versions of the php4Delphi Library from your Delphi 5 IDE.
You should also remove any previously compiled php4Delphi packages from your hard disk.
Select PHP version you are going to use. php4Delphi supports PHP 4.x and PHP 5.x, but not at
the same time. You have to compile php4Delphi for selected target version of PHP.
Use the "File\Open..." menu item in the Delphi IDE to open php4Delphi runtime package
php4DelphiR5.dpk. In "Package..." window click "Compile" button to compile the package
php4DelphiR5.dpk. Put the compiled BPL file into a directory that is accessible through the
search PATH (i.e. DOS "PATH" environment variable; for example, in the Windows\System
directory).
After you have compiled the php4Delphi run-time package you must install the design-time
package into the IDE.
2. Delphi 6.x:
Uninstall any previously installed versions of the php4Delphi Library from your Delphi 6 IDE.
You should also remove any previously compiled php4Delphi packages from your hard disk.
Select PHP version you are going to use. php4Delphi supports PHP 4.x and PHP 5.x,
but not at the same time. You have to compile php4Delphi for selected target version of PHP.
Use the "File\Open..." menu item in the Delphi IDE to open the php4Delphi runtime
package php4DelphiR6.dpk. In "Package..." window click "Compile" button to compile the
package php4DelphiR6. Put the compiled BPL file into a directory that is accessible through the
search PATH (i.e. DOS "PATH" environment variable; for example, in the Windows\System
directory).
After compiling the php4Delphi run-time package you must install the design-time
package into the IDE.
3. Delphi 7.x:
Uninstall any previously installed versions of the php4Delphi Library from your Delphi 7 IDE.
You should also remove any previously compiled php4Delphi packages from your hard disk.
Select PHP version you are going to use. php4Delphi supports PHP 4.x and PHP 5.x,
but not at the same time. You have to compile php4Delphi for selected target version of PHP.
Use the "File\Open..." menu item of your Delphi IDE to open the php4Delphi runtime
package php4DelphiR7.dpk. In "Package..." window click "Compile" button to compile the
package php4DelphiR7.dpk. Put the compiled BPL file into a directory that is accessible
through the search PATH (i.e. DOS "PATH" environment variable; for example, in the
Windows\System directory).
After compiling the php4Delphi run-time package you must install the design-time package into
the IDE.
4. Delphi 2005:
Uninstall previous installed version of php4Delphi Library from Delphi 2005 IDE.
Remove previously compiled php4Delphi packages from your hard disk.
Select PHP version you are going to use. php4Delphi supports PHP 4.x and PHP 5.x,
but not at the same time. You have to compile php4Delphi for selected target version of PHP.
Use "File\Open..." menu item of Delphi IDE to open php4Delphi runtime package
php4DelphiR2005.dpk. In "Package..." window click "Compile" button to compile packages
php4DelphiR2005.dpk.
Put compiled BPL file into directory that is accessible through the search PATH (i.e. DOS
"PATH" environment variable;
for example, in the Windows\System directory).
After compiling php4Delphi run-time package you must install design-time package into the
IDE.
5. Delphi 2006:
Uninstall previous installed version of php4Delphi Library from Delphi 2006 IDE.
Remove previously compiled php4Delphi packages from your hard disk.
Select PHP version you are going to use. Php4Delphi supports PHP 4.x and PHP 5.x, but not at
the same time. You have to compile php4Delphi for selected target version of PHP.
Use "File\Open..." menu item of Delphi IDE to open php4Delphi runtime package
php4DelphiR2006.dpk. In "Package..." window click "Compile" button to compile packages
php4DelphiR2006.dpk.
Put compiled BPL file into directory that is accessible through the search PATH (i.e. DOS
"PATH" environment variable; for example, in the Windows\System directory).
After compiling php4Delphi run-time package you must install design-time package into the
IDE.
5. Special Thanks
Daaron Dwyer
Toby Allen
Sébastien Hordeaux
Blake Schwendiman
Colin Nelson
Vasja Klanjšek
Mauro Diomelli
Kim Bracknell
Bart Libert
Peter Enz
Joao Inacio
Eddie Shipman
Thomas Weinert
Michael Maroszek
and all developers who sent me comments, remarks and bug reports.
6. Support
Since this is a freeware you are strongly encouraged to look at the source code and improve on
the components if you want to.
Of course I would appreciate it if you would send me back the changes and bug fixes you have
made.
7. Distribution tips
Written by João Inácio
Notice: i believe this can be used to deploy apps in any machine ,with or without php already
installed, without disrupting or altering in any way an already installed php.
edit php.ini:
extension_dir = "php\extensions"
if extensions require adicional dlls, they can either be thrown in %windir% or they're directory
should be added to "path" environment var.
8. FAQ
This error means, that an error occurs during the activation of PHP engine by psvPHP
component. For example, the component can not find php4ts.dll (php5ts.dll). You need to ensure
that the dlls which php uses can be found. php4ts.dll (php5ts.dll) is always used. If you are using
any php extension dlls then you will need those as well.
To make sure that the dlls can be found, you can either copy them to the
system directory (e.g. winnt/system32 or windows/system).
Copy the file, php.ini-dist to your %WINDOWS% directory on Windows 95/98 or to your
%SYSTEMROOT% directory under Windows NT, Windows 2000 or Windows XP and rename
it to php.ini. Your %WINDOWS% or %SYSTEMROOT% directory is typically:
c:\windows for Windows 95/98 c:\winnt or c:\winnt40 for NT/2000/XP servers
Module API=20020429 means that you compiled extension for PHP4, not PHP5.
PHP4 has API=20020429 and PHP5 20040412.
After you have to rebuild php4DelphiRx and php4DelphiDx packages and build the project
again. Because PHP4 and PHP5 are not compatible you can install PHP4Delphi only for one
specific version of PHP.
I need the ability to run a script in an external php file from my delphi program passign it a string
and returning another string. Is this possible with PHP4Delphi?
This is NOT a PHP Extension, just a regular Delphi program.
Example:
procedure TForm1.Button1Click(Sender: TObject);
var
phpVariable : TPHPVariable;
begin
phpVariable := psvPHP1.Variables.Add;
phpVariable.Value := 'initial value';
psvPHP1.Execute('myphpscript.php');
ShowMessage('My variable after execution ' + phpVariable.Value);
end;
• Return an array of strings from a PHP Library funciton
I need to return an array of strings from a PHP Library funciton. I know that I have to use the
_array_init ZEND API but again I'm lost as to how to do this.
key := 'name';
value := 'Valerie';
add_assoc_string(res, key, value);
key := 'city';
value := 'Leuven';
add_assoc_string(res, key, value);
end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, PHPCustomLibrary, phpLibrary, php4delphi, phpFunctions,
zendAPI, ZendTypes;
type
TForm1 = class(TForm)
psvPHP1: TpsvPHP;
PHPLibrary1: TPHPLibrary;
btnTarget: TButton;
btnExcute: TButton;
procedure FindAButtonExecute(Sender: TObject;
Parameters: TFunctionParams; var ReturnValue: Variant;
ThisPtr: Pzval; TSRMLS_DC: Pointer);
procedure ClickAButtonExecute(Sender: TObject;
Parameters: TFunctionParams; var ReturnValue: Variant;
ThisPtr: Pzval; TSRMLS_DC: Pointer);
procedure btnTargetClick(Sender: TObject);
procedure btnExcuteClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, PHPCustomLibrary, phpLibrary, php4delphi, phpFunctions,
zendAPI, ZendTypes, DelphiFunctions;
type
TForm1 = class(TForm)
psvPHP1: TpsvPHP;
PHPLibrary1: TPHPLibrary;
btnTarget: TButton;
btnExcute: TButton;
procedure btnTargetClick(Sender: TObject);
procedure btnExcuteClick(Sender: TObject);
procedure FindAndRegExecute(Sender: TObject;
Parameters: TFunctionParams; var ReturnValue: Variant;
ThisPtr: Pzval; TSRMLS_DC: Pointer);
procedure ClickMeExecute(Sender: TObject; Parameters: TFunctionParams;
var ReturnValue: Variant; ThisPtr: Pzval; TSRMLS_DC: Pointer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
end.