Helping ordinary people create extraordinary websites!

Building Desktop Applications in PHP

By Akash Mehta
2008-02-09

Building desktop applications in PHP

To demonstrate basic application development in PHP, we'll take a look at the PHP-GTK extension. PHP-GTK offers GTK bindings for PHP, allowing you to build scripts that create windows using the GTK graphics toolkit.

The desktop approach

As you can imagine, building a desktop application when coming from a web background is a whole new ballgame. Consider that, when you run a PHP script on the web, it goes from top to bottom. When you run a desktop application, you open it, and wait. And wait. And wait. It doesn't matter how long you wait for - you are waiting, and the application is not doing anything. Of course, the moment you click a button or open a menu, it springs into action again. At what point in the execution of a standard PHP script is absolutely nothing happening? (Besides in sleep()) .

Now, the only real way to achieve this in PHP is an infinite loop, with a check for some sort of change or input on every iteration. Which is exactly how the various most of the PHP-desktop systems available function. Of course, it would be impractical for you to code this loop manually, as there's a lot of code all over the place. Instead, you tell the interface system to draw your interface and bind your functions/methods to the interface elements. Drawing your interface involves telling it what interface elements to create - such as buttons and input boxes - and where to put them. Binding allows you to link your functions/methods to certain events for these elements. For example, you could create a button, and have one of your functions called when the button is clicked.

That's where the fundamentals end and the various differences in systems come into play. So, let's take a look at some code to create a basic desktop application.

PHP-GTK

PHP-GTK is one of the standard approaches for creating desktop-based PHP applications. As it only requires GTK for interface bindings, it's cross-platform - you can write your application on a Mac, test it on a Linux machine and deploy it to a Windows box, and with no code adjustments it will look (almost) exactly the same on each.

Before continuing, if you want to try out the code samples, you'll need a copy of PHP-GTK. You can download it from gtk.php.net; there are windows binaries, but for everything else you may have to use CVS. Installation instructions are available on the download page; you might find these instructions for Ubuntu useful as well.

Now, let's take a look at a basic PHP-GTK script from the manual. Here's the full code:

<?php
// Code snippet from the PHP-GTK manual.
if (!class_exists('gtk')) die("Please load the php-gtk2 module in your php.ini");
$window = new GtkWindow();
$window->set_title('Our PHP-GTK window');
$window->connect_simple('destroy', array('gtk', 'main_quit'));
$label1 = new GtkLabel("Hello world!");
$window->add($label1);
$window->show_all();
Gtk::main();
?>

As you can see, it's perfectly normal PHP code. However, it's relatively short; it ends with a call to this mysterious Gtk::main(). Remember that infinite loop I mentioned? When you finish creating your window and drawing it, your scripts hand over to the GTK library that handles that same loop. Still, it's new territory, so let's step through the code and review it.

if (!class_exists('gtk')) die("Please load the php-gtk2 module in your php.ini");

Pretty standard stuff: we check that the 'gtk' class is available. Without it, you won't be able to render your windows with the GTK toolkit. As PHP isn't really designed for desktop applications, there is still the potential for deployment quirks such as lack of the GTK module, so we carefully check it here.

$wnd = new GtkWindow();
$wnd->set_title('Our PHP-GTK window');

The GtkWindow class is, as it's name suggest, a GTK window. Working with GTK in PHP is heavily object oriented; you'll have objects for your windows, the interface elements on those windows, GTK itself and more. The set_title() method sets the title, similar to using a <title> tag in HTML, with the exception that what really goes in the title bar is no longer at the mercy of the end-user environment.

$window->connect_simple('destroy', array('gtk', 'main_quit'));

This is where we start binding to the interface. When the user clicks the close button on our application, it doesn't just magically clean up after itself and quit. The operating system can't simply kill our application; chances are we want to tie up some loose ends and clear out data we've stored. As a result, we have to link the 'destroy' signal to the gtk::main_quit() method. This method essentially ends our application so that we can cleanly exit.

$label1 = new GtkLabel("Hello world!");
$window->add($label1);

Now we start creating our interface. We'll start with a simple label. In desktop applications, if you want to cleanly position text (and you do, as opposed to running it all from the top-left corner of the form) it needs to be in some sort of container, and a label is the common term for a basic text container in a window.

You can position your label, give it some additional properties and so on, but for now we'll just give it the text "Hello world!". By calling the add() method of our GtkWindow object $window, we tell GTK to put our new label on our window. Without this, the label can't really appear anywhere. Interestingly, this approach of not scoping an interface element within a particular form makes element reuse simple and effective.

$window->show_all();
Gtk::main();

This is the interesting part. First of all, our window won't actually appear until we call the show_all() method on our window's object. This call tells GTK to show the window on the screen. However, that infinite loop I mentioned before? Now that we've finished drawing our window, we're ready to roll. We call Gtk::main(), and away we go...


Your first PHP-GTK application!

If you're trying to run this application, I presume you're running it from the command line with PHP-CLI. To check that everything's in order, click the 'x' button in the top-right. Your PHP-GTK application should close, and your console window should indicate that the program has finished and take you back to the prompt.

With PHP-GTK, you have all the power of the GTK toolkit, allowing you to create complex interfaces and high-end applications. Check out the PHP-GTK manual for some more complex tutorials. There are also some good tutorials elsewhere online.




Tutorial pages:

 5 Votes

You might also want to check these out:


Leave a Comment on "Building Desktop Applications in PHP"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS