
|
|
|||
Building Desktop Applications in PHPBy Akash Mehta2008-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 approachAs 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 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-GTKPHP-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 $window = new GtkWindow(); $label1 = new GtkLabel("Hello world!");
$window->show_all(); As you can see, it's perfectly normal PHP code. However, it's relatively short; it ends with a call to this mysterious 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(); The $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 $label1 = new GtkLabel("Hello world!");
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 $window->show_all(); This is the interesting part. First of all, our window won't actually appear until we call the
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: » Introduction » Fundamentals » Benefits of desktop development » Building desktop applications in PHP » Further reading |
||||
| About the NetVisits, Inc Network | Write For Us | Advertise Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy. |
Visit other NetVisits, Inc. sites: |