Developing Desktop Applications in PHP for Beginners.

December 18, 2007

Tutorials

Developing Desktop Applications in PHP for Beginners.

Introduction:

We have been creating web-applications using PHP since it came into existence, now we can also develop Desktop or Stand alone Applications with PHP-GTK. Desktop Applications are one which does not need either a Web Server like IIS, Apache, PWS etc. or a web browser for their execution. One of the reasons why Java is so popular is because it can be used to build applications, web pages, applets and beans that can run on several platforms including Windows, Linux and Solaris. Java runs a virtual machine called JVM, and code is compiled into an intermediate format known as Java byte code, which is platform independent. When that particular piece of code is executed within the JVM, the JVM optimizes the code for the particular platform on which it is running as it is being compiled.

Microsoft’s latest technology, .NET follows the same principles. Code is compiled into Microsoft Intermediate Language (MSIL) and is then executed within the .NET framework as an application domain. Microsoft is hoping to standardize C# and MSIL so that .NET code can run cross platform.

So what has all of this got to do with PHP-GTK? Well, both Java and .NET can be used to build windowed applications and web pages. Thanks to PHP-GTK, we can now build cross platform windowed applications with PHP as well.

What is PHP-GTK?

GTK is an acronym for the GIMP Toolkit and GIMP is an acronym for GNU Image Manipulation Program, and is a fully featured graphics editing program that runs on Linux. It has many (if not all) of the features of popular Windows programs such as Photoshop and Paint shop. It’s the graphics editor of choice for most Linux users.

GTK is actually part of a set of libraries that was written in C called GTK+. GTK+ was built up over time and is now a main part of Gnome, which is a Linux GUI desktop environment. GTK+ is based on an object-oriented nature and also includes two other libraries:

  1. GLib: A library of tools that can be used to assist developers when creating applications with GTK+.
  2. GDK: Similar to GDI for Win32, GDK standard for GIMP drawing kit and wraps a set of lower level drawing functions into classes that make developing applications with GTK+ easier. If you’re thinking along the lines of MFC for C++ then you’re making a fair comparison: MFC wraps several controls and hides the calls to the underlying Windows API’s from the developer. GDK does the same thing for GTK+.

Where to get?

We can download binary as well as source code version of PHP-GTK from http://gtk.php.net/download.php . As a beginner, it would be a difficult process to download and install in this manner. Where we need to set up another php.ini file for PHP-GTK. Instead there is another way of installing it. We can get PHP-GTK2 in an executable form as we get WAMP.EXE. (Windows, Apache, Mysql, PHP) All we have to do is just download just download the files from http://www.gnope.org/download.php ,unzip them and double click on the icon GnopeSetup-1.5.1.exe . It will run through a step by step process where it will set up PHP-GTK automatically.

How to test the installation?

Once the installation is done we would eager to know about what is special in it? When we install PHP we would run phpinfo () from root directory .For this let us run a sample script which displays Hello world (as usual) .we can use Dreamweaver for editing the code. Another important point to be kept in mind is to save the file with extension .phpw it can be saved anywhere on your hard disk.

Here is the sample code:

<?PHP
if (!class_exists('gtk')) {
    die("Please load the php-gtk2 module in your php.ini\r\n");
} 
$wnd = new GtkWindow();
$wnd->set_title('Hello world');
$wnd->connect_simple('destroy', array('gtk', 'main_quit')); 
$lblHello = new GtkLabel("hello world");
$wnd->add($lblHello); 
$wnd->show_all();
Gtk::main();
?>

I have saved this sample file with the name hello.phpw at c:\test\. We should run this sample code from command line interface. (CLI) There may be a question rising in your mind asking, why we should run through command prompt than by just by double clicking it as it is a stand alone application. It is possible, for that we need to have a PHP compiler which converts our PHP-GTK code to EXE file . For now, let us try running it from the command prompt.

Steps for Executing a sample code:

  1. Start -> Run -> cmd (for xp sp2 and later version) or command (for windows 98).
  2. Now you could see a black window which is Command Prompt. Key in the following commands as shown below.

	 z:>c:
         c:> cd test
         c:\test>php hello.phpw

Once we finish this line and hit the Enter key, we should see the desired output.

This shows the successful installation of PHP-GTK2 and shouls us PHP output without a web-browser.

Some interesting Websites on PHP-GTK:

  1. http://www.kksou.com/php-gtk2/
  2. http://phpgtk.activeventure.com/gtk/gtk.gtkbox.html (describes about all base classes).
  3. http://gtk.php.net/download.php
  4. http://www.gnope.org/download.php

3 Responses to “Developing Desktop Applications in PHP for Beginners.”

  1. jacobfogg Says:

    I am very excited about this! PHP was the first REAL programing language I have used (since version 3), and though I have learned several others, it’s always been my favorite! I am anxious to learn how php desktop apps are able to tie into their web based brothers… could this be a solution to bridge the gap between offline usage and online expandability?

  2. _____anonymous_____ Says:

    I encountered an exception when clicking the PHP-GTK tag link on the tutorial about Developing Desktop Applications. The exception message complains about the – being an invalid character.

  3. carlosbuenosvinos Says:

    This Tool is great for little companies that have part of some Customer Business Process based on PHP and they don’t need to concentrate of all their customers on their resources, they can delegate part of those processes on their custormer resources.

    Also, talking about Teaching the way users have to use new tools developed to them, it’s more easy to look at Windows-Based Systems than Web-Based Systems.

    I think we will hear much more about this "new" usage of PHP.