Component Development in Delphi For PHP
Component Development in Delphi For PHP
Guest blogger: This article was written by Jim Tierney, Principle Engineer working on the BDS
development team at CodeGear. I work closely with Jim on the ASP.NET designer.
I�ve been trying out Delphi for PHP. This article provides an overview Delphi for PHP and how
I�ve used it.
My Project
I�ve implemented a simple poll (or survey). The page to test my classes looks like this:
Vanilla PHP
I started my project by building PHP classes and a test page without any dependence on the VCL for
PHP framework. The key Delphi for PHP features that I used for this purpose were the debugger, code
editor (with syntax highlighting and code completion), and project manager.
1 <?php
2 require_once("..\..\pollClasses.php");
3 ?>
4 <html>
5 <head>
6 <title>Poll Test</title>
7 <link rel="stylesheet" href="pollStyles.css" type="text/css" />
8 </head>
9 <body>
10 <form method="post" action='<?php echo htmlentities($_SERVER['SCRIPT_NAME'])
?>'>
11 <?php
12 $pollController =
PollController::loadPoll(simplexml_load_file('pollDescription.xml'));
13 if (!empty($pollController))
14 {
15 $pollController->handleRequest();
16 if (!$pollController->isPollDone())
17 $pollController->pollView->show();
18 else
19 echo "Done";
20 }
21 else
22 echo "No poll";
23 ?>
24 </form>
25 </body>
26 </html>
Once my test page was working, I was ready to wrap my poll classes in a VCL for PHP component.
����
Class
1 <?php
2 require_once("vcl/vcl.inc.php");
3 use_unit("controls.inc.php");
4 use_unit("pollSample/poll/pollClasses.php");
5
6 class PollControl extends Control
7 {
8 ...
Properties
My poll classes load a poll description from a PHP simplexml object. To allow the XML to be
specified at design time, PollComponent declares two published properties. XMLFileName is for
specifying a filename. XMLLines is for typing in XML.
18 // XMLLines property
19 function getXMLLines() { return $this->_xmlLines; }
20 function setXMLLines($value) { $this->_xmlLines = $value; }
21 function defaultXMLLines() { return array(); }
22
23 // XMLFileName property
24 function getXMLFileName() { return $this->_xmlFileName; }
25 function setXMLFileName($value) { $this->_xmlFileName = $value; }
26 function defaultXMLFileName() { return ""; }
Events
PollComponent declares a single published event. OnPollDone is an event that is fired when the user
has completed the poll.
13 // OnPollDone event
14 function getOnPollDone () { return $this->_onPollDone; }
15 function setOnPollDone ($value) { $this->_onPollDone = $value; }
16 function defaultOnPollDone () { return null; }
Property Editors
The XMLFileName property can be set using a file open dialog. The XMLLines property can be set
using a multiline edit. The following code registers property editors for XMLFileName and
XMLLines.
11
registerPropertyEditor("PollControl","XMLFileName","TFilenamePropertyEditor","nativ
e");
12
registerPropertyEditor("PollControl","XMLLines","TStringListPropertyEditor","native
");
Component registration
The following code registers PollComponent so that it will appear on the tool palette under the
�Poll� category:
9
registerComponents("Poll",array("PollControl"),"pollSample/poll/poll.inc.php");
Using the Component
The first step in using the component is to install the component package with the
Component/Packages... command.
As a result of installing the package, PollComponent is registered and appears on the tool palette:
A PollComponent can be drag dropped from the tool palette onto a form:
The PollComponent class is coded to display �(empty)� unless an XML description of the poll has
been provided. Here is the code to generate the component markup:
51 function dumpContents()
52 {
53 $this->checkPollController(); // load poll if necessary
54 echo "<div id=\"" . $this->Name . "_div\" style=\"width: " . $this->Width .
"px; height: " . $this->Height . "px\">";
55 if (!empty($this->pollController))
56 {
57 if (!$this->pollController->isPollDone())
58 $this->pollController->pollView->show();
59 }
60 else
61 echo "(empty)";
62 echo "</div>";
63 }
An XML description is provided by editing either the XMLLines or XMLFileName properties using
the object inspector.
The PollComponent looks better after adding a StyleSheet component..
Finally, an event handler for the OnPollDone event is created using the object inspector. I've added
code to redirect to another page.
Conclusion
For vanilla PHP development, Delphi for PHP provides a code editor, debugger, and project manager.
For visual development, Delphi for PHP provides a familiar combination of framework and tooling.
To integrate vanilla PHP with VCL, consider wrapping PHP classes in VCL components.