New Tool Brings Richer UIs to PHP Web Apps
After several years in development, PHP-GTK 1.0 was recently released for free download This week, IDN focuses on the PHP-GTK extensions for PHP, which enable enterprise devs to employ PHP in more varied ways, notably to move away from the Web and use more traditional GUI technologies. Get the download, some sample apps and some great insights on using PHP-GTK for your next enterprise PHP project.
After several years in development, PHP-GTK 1.0 was just released in late October for free download. The code is available from the official PHP-GTK website. (Here is one direct link to the download).
Why would an enterprise dev want to download PHP-GTK 1.0?
Most PHP scripts are designed to be executed from the top to the bottom each time the script is requested by a client (web browser). Programming GUI applications with PHP-GTK is different from these typical PHP projects, as they are event driven. That means that the PHP-GTK script stays in memory until the user decides to quit (close window event). Each PHP-GTK script has three parts:
- Construct the GUI elements. Windows, buttons list, etc.;
- Functions that will be executed on events; and
- A main loop to process events.
PHP was designed and is still maintained and developed as a server-side scripting language. Like many other languages, it can also be used as a scripting language or command line to perform complex tasks that would be difficult to solve with shell scripts. Other languages (Tcl, Python, etc.) provide bindings for GTK+, allowing the developer to create GUI applications that run on the desktop without the need for a web server.
Also, in a Sitepoint interview last year, PHP-GTK's co-creator Andrei Zmievski said the main advantage that PHP-GTK provides devs is "another way to control the back-end system, unencumbered by the interface limitations imposed by HTML and the browser." Such features as drag-and-drop, grids, tree views, etc. are much more powerful than their in-browser counteparts and can be a boon to productivity, Zmievski added.
Among those early PHP-GTK users, Zmievski said, many used it simply so they could continue to use their favorite language to build solutions on the client side, and that otherwise they would have had to resort to Java or something similar. "To me," he added, "it means that PHP-GTK is filling a much-needed niche."
As an extension for PHP, PHP-GTK implements language bindings for the GTK+ toolkit. It provides an object-oriented interface to GTK+ classes and functions and greatly simplifies writing client-side cross-platform GUI applications. [GTK+ was initially developed for and used by the GIMP, the GNU Image Manipulation Program. Therefore, it is named "The GIMP Toolkit" so that the origins of the project are remembered.] Today, GTK+ is used by a large number of applications, and is the toolkit used by the GNU project's GNOME desktop.
Providing PHP Improved GUI Fuctions
With the ability to execute PHP scripts on many different platforms and with the broad selection of extensions in form of PEAR classes and extensions written in C/C++, it was a natural step to provide bindings to GTK+.
PHP-GTK was created as an extension that enables the developer to create GUI applications with the classes and functions provided in the GTK+ library. The GTK+ library has been ported to several platforms, including Windows and Linux, providing the same look and feel on all platforms. In specific, the PHP-GTK extension has been created so it can be loaded by the CGI or CLI versions of PHP, so there is no need for any web servers. The extension requires that the GTK+ runtime libraries be installed on the system.
PHP-GTK extends PHP, but PHP-GTK itself can also be extended with new features. One of these extensions, libGlade, makes it possible to create all GUI elements from a single XML file. This reduces the required number of code lines and makes it possible to use programs as Glade (wGlade on Windows) to design advanced user interfaces without writing a single line of code.
Other extensions include tools to:
- Render HTML GtkHTML);
- Create advanced editors (Scintilla);
- Plot data (Extra); and
- Manipulate images (GtkPixbuf), drawing (Canvas) and more.
Using PHP-GTK for GUI Apps
PHP applications are usually installed on a web server and accessed with a browser through a network connection. In this case, there is no need to install PHP on the client machines. Some features within applications (keeping files or database connections open for a long time or offline access to the application) are difficult or impossible to implement with standard web technology. In this case, PHP and PHP-GTK should be installed on the client machine.
The difference between web technology and client GUI applications can be visualized through the types of events they handle.
Web servers listens for requests (GET, POST, PUT) and in response, read a document from the disk and return it to the client. The returned document can be parsed with PHP to generate a completely new document.
GUI applications can be designed to handle many different types of events (button click, menu click, network activity, timers, etc.). These events can be activated by the user, the program it self or other programs on other systems.
The list of applications created with PHP-GTK is very long and includes editors, IDEs, IRC chat clients, reporting tools, test suites and games. Notable enterprise-level apps include:
- Nova -- An Open Source peer-to-peer client based on GnucDNA and using the Gnutella network. Download here
- Debbi -- Debian block installer -- A front end to install programs for system administration/run programs-scripts. Mainly for the Debian system, but can be used on other systems. Download here.
- PriadoBlender -- A tool to let you distribute your PHP scripts as executable EXEs (similar to the PHPCompiler). Unlike a true compiler, however, PriadoBlender takes your PHP code and the PHP interpreter itself and blends them into stand-alone PHP executables. Download here.
- XPM Browser -- A tool that lets you view your collection of XPMs with ease. The scrolled window loads the images and their names, and directories are shown as buttons -- simply click to change directories. The console is also present, with DIR/LS and CD commands. Download here.
Frank M. Kromann, M.Ss.E.E. is Senior Software Engineer at intelleFLEET, developing PHP and C/C++ applications on several platforms. He has developed Web applications since 1994 and has been a member of the PHP development team since 1997.