By: Tim DelChiaro
Abstract: How to create an extension with Delphi and configure Delphi for PHP to use it
PHP has the ability to be extended using code from other languages. This can be done using extensions, which are nothing but a dynamic library. So, on Windows, you can write your own extensions using Delphi for Windows, and in fact, is very easy, because of a great library, called php4Delphi. This library is also used by Delphi for PHP IDE to interact with PHP engine, so you can be sure works very well.
In this article, I’m going to show you how to create such extension and how to configure Delphi for PHP to use it. I’m going to use Delphi 2009 and a special version of php4Delphi, that works with Delphi 2009.
You can download it from here:
Check the documentation on how to install it, but basically are two packages, one is the runtime package, and the other is the design time one.
Once it’s installed, you have a new item on the repository, called PHP Extension:
If you select it, a new .dll project will be generated, with a .dproj and a .pas file, save everything on a folder of your choice, we are going to use the ‘myextension’ name for everything.
IMPORTANT: Be sure to check the unit just created is part of the project, because it seems there is some kind of bug and the unit is not added to the project.
Now is time to add the functions we want to use on PHP, this can be done by editing the Functions property of the extension module, check this shot:
This is a collection editor where we can add new functions, so clicking on the new button, will create a new one, and we can set all its information
Each function has the Parameters property, another collection we can edit and set the name for the parameter and also the expected parameter type.
Once the function is created, to write the working code for it, just generate the function’s OnExecute event handler, on this case, we are going to return the length of the first parameter:
It is convenient to set the project to generate the .dll in the extension folder PHP uses. This can be set using the project options dialog:
So now is time to tell the PHP engine the IDE uses to load this extension. The first thing to do is to ensure the IDE is closed, if not, the changes we perform are not going to work. To tell PHP to load an specific extension, we need to edit the php.ini file, but the IDE uses a template file to generate this one, so this is the file we need to edit. It’s located by default on C:\Program Files\CodeGear\Delphi for PHP\2.0\php, you can use a text editor to open this file, and then, search for ";extension=php_bz2.dll", this will point you to the zone of the file where all extensions are loaded. Now you need to add a new line:
Now, when you load the IDE, your new function will be ready to be used and you can write code like this:
If you run this script, produces the right result, 14
It is also possible to pass objects from Delphi for Windows to Delphi for PHP, in this case, you need to cast them to integer, so they will be used as handlers in PHP code. Then, when you want to send it to Delphi for Windows to perform operations, simply send the integer value and cast it to the right type of object.
This is a nice way to provide web interfaces to your existing applications, and also, a nice way to extend your PHP applications using third party functionality.
This article is a reprint of a blog post by José León, Delphi for PHP Architect. You can read more of his blog posts at http://blogs.embarcadero.com/joseleon/.
Download Delphi 10 now!
Webinars on demand!
More social media choices:
Delphi on Google+
@RADTools on Twitter
Server Response from: ETNASC04