Status of PHP Toolkit


Contents

Introduction
Requirements
Installation
Examples
Deploying VCL for PHP apps
Configuring Lazarus as a PHP IDE
FAQs
Developing with VCL for PHP
To Do


Introduction

These notes describe the status of the PHP Toolkit, a set of utilities for using your Object Pascal forms and code with modern open-source frameworks like PHP, VCL for PHP and PHP4Delphi to create Web applications.

With the PHP Toolkit you can do three things:

  1. Automatically convert Delphi or Lazarus forms (.dfm/.lfm) to PHP code (.php) that uses the open-source VCL for PHP class library. This is a simple way to preserve some of the forms' visual layout in a Web app.

  2. Configure Lazarus for use as a PHP IDE, including syntax checking of PHP and JavaScript code and jumping to syntax errors.

  3. Create PHP Extension libraries from Pascal code using units from PHP4Delphi and the Toolkit's ExtPZAPI unit. This is an easy way to add non-VCL/non-LCL Pascal code to a PHP Web app. The Pascal library can be called by and call back to the PHP code created from the form in (1).

The PHP Toolkit can be downloaded from here: http://web.fastermac.net/~MacPgmr/PhpTk/downloads

A discussion forum is here: http://groups.google.com/group/phptk

You can contact the author at:  MacPgmr (at) fastermac (dot) net

Author:  Phil Hess.
Copyright:  Copyright (C) 2008 Phil Hess. All rights reserved.
License:  All source code developed for the PHP Toolkit is released under the Modified LGPL license.


Requirements

  1. Web server

    You can use a local Web server for testing.

  2. PHP

    You will need PHP 5.1.0 or later. The Toolkit was developed and tested with PHP 5.2.4.

  3. VCL for PHP

    VCL for PHP is the open-source PHP class library that is used to run forms converted to PHP. The Toolkit was developed and tested with VCL for PHP 2.0, build 2008-05-19, available here:

    http://www.qadram.com/vcl4php

    Note that if you have CodeGear's Delphi for PHP (version 2.0), you already have this release of VCL for PHP.

    http://www.codegear.com/products/delphi/php

  4. Pascal compiler

    You will need one of these compilers:

  5. Rhino and JSLint (optional)

    If you intend to configure Lazarus as a PHP IDE, you'll need the Rhino JavaScript interpreter for Java (js.jar) and the JSLint JavaScript Verifier script (jslint.js):

    http://www.mozilla.org/rhino

    http://www.jslint.com/rhino/index.html

    These two files are used in conjunction with the CheckJS utility included with the PHP Toolkit. Place js.jar and jslint.js in the same folder as the compiled CheckJS.

  6. Browser debugging tools (optional)

    If you use Firefox, you'll find the following add-ons useful:

    If you use Safari on OS X or Windows, you can activate similar tools:

    Finally, if you use iCab on OS X, you'll find several developer-oriented tools on its Tools menu:

  7. PHP4Delphi framework (optional)

    If you want to learn more about writing PHP extensions or using PHP with Delphi, you will want version 7.0 or later of Serge Perevoznyk's PHP4Delphi, available here:

    http://sourceforge.net/project/showfiles.php?group_id=48329&package_id=94501

    Serge's home page is here:

    http://users.telenet.be/ws36637/php4delphi.html

    Note: PHP Toolkit includes the PHP/Zend types units from PHP4Delphi, but the Toolkit's ExtPZAPI unit only includes the minimum PHP/Zend API functions required for the example PHP extension. PHP4Delphi includes the full PHP/Zend API, plus components for using PHP with Delphi.


Installation

  1. Unzip or download the Toolkit source files into their own folder, preserving the subfolders.

  2. Open a terminal window and change to the Toolkit folder, then compile the FmToPhp converter.

    With Delphi:  dcc32 fmtophp.dpr

    With Free Pascal:  fpc fmtophp.dpr

    Tip: You can also compile from the IDE. With Delphi, open file fmtophp.dpr; with Lazarus, open file fmtophp.lpi.

  3. The compiled converter (fmtophp.exe on Windows or fmtophp on OS X and Linux) can then be placed along with its fmtophp.ini configuration file anywhere you want.

  4. To see FmToPhp's syntax, simply run it in a terminal window without any parameters or switches. For example:

    With Windows:  fmtophp

    With OS X and Linux:  ./fmtophp

  5. If you intend to configure Lazarus as a PHP IDE, compile these two console apps with Free Pascal:

      fpc checkphp.pas
      fpc checkjs.pas


Examples


Deploying VCL for PHP apps

To deploy locally, copy the entire vcl folder to the location described above in the local Web server requirements.

To deploy to a Web site, copy the entire vcl folder to your server's deployment folder. If you don't have much space on your server, then at a minimum create a folder named "vcl" in your Web server's deployment folder and copy all of the VCL for PHP .inc.php files to it, as well as its qooxdoo, js and xajax folders.

Tip: You can set the vcl folder permissions so that only the owner has read privileges. That way it won't be visible to others via their browser.

If you're also deploying Components 4 PHP Lite, create a folder named "components4phplite" in the vcl folder and copy its .inc.php files to the components4phplite folder. Also copy its themes folder to the components4phplite folder. If you're deploying Components 4 PHP Full, the folder name is components4phpfull.

Copy files like hello.php, hello.xml.php and helloajax.php to your Web server's deployment folder (one level up from the vcl folder you created).

Copy any PHP extension libraries that your app needs to your Web server's PHP extensions folder.

Important! If your Web server has disabled the dynamic loading of extensions (via the PHP "dl" function), you will have to load your extensions when PHP starts up via its php.ini file. To determine if "dl" is disabled, run this PHP script on your Web server:

  <?php phpinfo() ?>

Then search for "enable_dl" and look at its setting.


Configuring Lazarus as a PHP IDE

If you're using Windows you can purchase CodeGear's Delphi for PHP, which includes a Delphi-like IDE for designing VCL for PHP pages visually and writing, running and debugging PHP code. CodeGear also has a free 30-day trial version.

You can also use Lazarus for basic VCL for PHP development by adding several external tools to its Tools menu. Choose Tools | Configure custom tools, then click the Add button to add the following commands to the Tools menu:

You can now use these custom tools as follows: What we're doing here is using Lazarus to design our Web forms visually, then running the converter and syntax checkers as needed. The .pas and .lfm files created by Lazarus will not be used in deploying your Web app and are only used to contain the original design of the forms.


Frequently Asked Questions

  1. What exactly is "VCL for PHP"?

    Here's as good a definition as any, from Steve Cooney:

    "[VCL for PHP] is a server side class library written in PHP that emits HTML/XML/Javascript that is consumed by a DHTML enabled browser."

    VCL for PHP has classes, properties, events and methods that are very similar to Delphi VCL and Lazarus LCL classes, properties, events and methods, which make it easy for the Delphi/Lazarus programmer to learn.

    The VCL for PHP documentation is here:

    http://www.qadram.com/vcl4php/vcldoc/index.html

  2. How do I get started using PHP and VCL for PHP?

    The FmToPhp converter creates a working PHP app from your form that you can run with VCL for PHP without any programming on your part. Of course, the app it creates is just a pretty face and doesn't do much, but you can start adding code to the event handlers to get your feet wet.

  3. Where can I ask questions about VCL for PHP?

    Try one of these two forums:

    http://forums.delphi-php.net

    http://www.qadram.com/vcl4php/forums

  4. How many languages do I have to know to use VCL for PHP?

    Let's assume you already know Object Pascal and user interface design through working with Delphi or Lazarus. PHP isn't hard to learn, although the syntax is more like C than Pascal. Since PHP is dynamic and interpreted, you can start adding code to your converted forms without worrying about compiling and linking. Just add the code and run it.

    PHP makes a good Web server language for a number of reasons:

    The PHP manual is here:

    http://us.php.net/manual/en/index.php

    Developing any Web app that uses Ajax means you'll need to learn a bit of JavaScript at some point since that's the language that the browser understands. One advantage to learning some JavaScript is that it allows you to do more on the client side without sending so many requests to the server, thereby speeding up the app as far as your users are concerned. You can output JavaScript from your PHP code using echo or you can drop "out" of PHP and write JavaScript code directly. See Example 1 above for examples.

    Documentation for JavaScript and the browser's DOM objects is available here:

    http://developer.mozilla.org/en/JavaScript

    Another handy reference is here:

    http://www.w3schools.com/jsref

  5. What do you mean by "non-VCL/non-LCL Pascal code" in extensions?

    You don't want to include any VCL or LCL code in your extension since the extension will be executing on the server and not the desktop. This means you shouldn't use any units like Forms, Dialogs, Controls, StdCtrls, ExtCtrls, Buttons, ComCtrls, etc. or any units that depends on these units. You should only use non-visual RTL units like SysUtils, StrUtils, DateUtils, Variants, Classes, Math, etc. You can also use the Windows unit on Windows and the Free Pascal Unix unit on OS X and Linux.

  6. I have an older version of PHP. Can I still use it?

    The PHP Toolkit does not support PHP 4. VCL for PHP was originally developed with PHP 5.1.3 so you should have at least that version or later if you're working with VCL for PHP. Delphi for PHP 2.0 includes PHP 5.2.2.

    If you're developing PHP extension libraries and you don't have at least PHP 5.2.0 you will need to change some defines in PHP.INC.

  7. I don't think the PHP Toolkit is for me. What are my other options?

    If you're still intending to work with Pascal, here are a couple of other things to look at for Web development:

  8. My application uses TStringGrid. What's the equivalent in VCL for PHP?

    VCL for PHP does not have an equivalent control. However, you can use the JTGrid control from the inexpensive Components 4 PHP Lite from JomiTech:

    http://www.jomitech.com


Developing with VCL for PHP

What is "Ajax" and how does VCL for PHP actually work?

Ajax stands for Asynchronous JavaScript and XML and it's a clever technique for making your Web app feel more like a desktop app. But before we discuss how VCL for PHP uses Ajax, let's talk about how a Web app is constructed with VCL for PHP.

When a .php page is loaded on the Web server, the server executes the page's PHP code with its PHP interpreter module. The VCL for PHP code included at the top of the .php file loads and processes the page's .xml.php file, generating HTML and JavaScript that the server sends to the browser on the client computer. Any JavaScript event handlers in the PHP code are also executed in order to generate the JavaScript event handling code. It's this HTML and JavaScript that the user sees and interacts with. In other words, the HTML and JavaScript running in the browser is your Web app's user interface, not the PHP code that generated it. You can see this auto-generated HTML and JavaScript by looking at the loaded hello.php page's source in your browser.

While developing a VCL for PHP Web app you might start thinking of the PHP code you write as the user interface, but it's not. It's easy to make this mistake because the HTML and JavaScript generation is done for you automatically. Sure, you can output JavaScript yourself in your PHP code if you need to (using echo), but initializing controls is mostly done behind the scenes. More accurately, the PHP code is really two things: (1) the generator of HTML and JavaScript and (2) the handler of requests passed to it via Ajax.

In a traditional HTML page that includes a button, clicking the button submits the page's form to the Web server, which generates a new page based on the form's data that it sends back to the browser. This results in an unsightly redraw of the entire page and can also involve a delay before "control" is returned to the user if generation or transmission of the new page takes time. With Ajax, when you click the button, the JavaScript event handler sends a request to the PHP code on the server, but doesn't submit the form and doesn't wait around for the server to respond to its request (why it's called "asynchronous"). Instead, the XML and JavaScript sent back by the server is handled by a JavaScript "listener" that can update any part of the page. This results in a smoother page refresh and generally feels more responsive to the user.

Note that while your Web app's user interface runs in the client's browser, most of the app's actual work is typically done on the server. For example, the server-side PHP code might read and write data, do database queries, generate a report, run a computational model, etc. when requested by an Ajax call. Much of this processing could be done in a PHP extension library that you develop with Pascal. Remember that the browser JavaScript code will not have access to files or services or programs on the client's computer, so this sort of processing will need to be done on the server.

PHP Toolkit's approach to Ajax

VCL for PHP components have an ajaxCall method that you can use in your JavaScript event handlers to generate JavaScript code that makes an Ajax call to server-side PHP code that you include in your .php page file. However, this approach can be a bit slow, so the FmToPhp converter by default generates a JavaScript function (doAjax) that you can use to call server-side PHP scripts. Scroll down to the bottom of the converted hello.php file to see the JavaScript code that FmToPhp generates. The Toolkit's approach uses the browser's JavaScript XMLHttpRequest object to make the Ajax call, as described here:

http://developer.apple.com/internet/webcontent/xmlhttpreq.html

Note that you can use the VCL for PHP ajaxCall approach if you want. Just include the -c switch when converting your forms. When the -c switch is used, FmToPhp does not generate the XMLHttpRequest-based JavaScript code; instead, it outputs ajaxCall with each JavaScript event handler and a corresponding PHP event handler where you put your PHP event handling code. For more information on ajaxCall, refer to VCL for PHP documentation as this approach is beyond the scope of these notes.

Think client-server

Like a client-server application, a Web app's code is running on two different computers. The PHP code and any PHP extensions you develop are running on a Web server and JavaScript code is running in the user's browser. Furthermore, the server-side PHP generates and sends much of the JavaScript that the browser runs. Ajax techniques are used to communicate between the browser and the Web server.

Don't confuse the two places where code is running. Although your main PHP form may contains a mix of PHP and embedded JavaScript, the PHP only runs on the server and the JavaScript only runs in the browser. You may find it easier to put mostly just JavaScript event code in your main PHP form, along with any one-time PHP code in the form's PHP Create handler. The rest of your app can then be made up of separate PHP files that are run via Ajax or some other technique.

One implication of this client-server split you may find confusing is that the names of the VCL for PHP component properties differ somewhat from the JavaScript DOM and qooxdoo object properties. For example, a reference to the main form's Caption in PHP would look like this:

  $this->MainForm_Window->Caption

Whereas a reference to it in JavaScript would look like this:

  MainForm_Window.getCaption()


To Do


Last updated: Sept. 2, 2008