Article

Home » Server-side Coding » PHP & MySQL Tutorials » Build Cross-Platform Windowed Apps with PHP

About the Author

Mitchell Harper

author_mitchellharper Mitchell is the lead developer for Interspire, who develop re-brandable Internet software and tools to help Web developers increase their customer base and make more revenue. Mitchell can be reached via email at mitchell@interspire.com.

View all articles by Mitchell Harper...

Build Cross-Platform Windowed Apps with PHP

By Mitchell Harper

March 20th, 2002

Reader Rating: 9

Page: 1 2 3 4 5 6 7 8 Next

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 platform on which it is running as it is 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.

In this article we're going to take a look at PHP-GTK: what it is, how to download and install it, and how to use it to build cross-platform interactive GUI applications. To test the PHP-GTK samples in this article, you should have PHP 4.1.1 or later installed on a Windows, Linux or Unix machine.

What is PHP-GTK?

Personally, I'd call PHP-GTK an excellent set of classes that can be used to build applications that contain buttons, windows, toolbars, menus, scroll bars, database access, lists and more. The team that developed PHP-GTK defines it as a PHP extension that allows us to easily write cross-platform GUI based applications.

Funnily enough, PHP-GTK was written to prove that PHP could actually be used to build stuff other that Web pages, and let me tell you that they've succeeded.

No doubt you're familiar with the PHP acronym, but what does GTK mean? GTK stands for GIMP Tool Kit, and it's basically just a set of widgets that we can use to create applications. A widget is the equivalent of a control (such as a button, list box, frame or radio button) in "Linux speak".

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 Paintshop. 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+ has an object-oriented nature and also includes two other libraries:

  1. GLib: A library of tools that can be used to help developers create applications with GTK+.

  2. GDK: Similar to GDI for Win32, GDK is the 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+.

Although GTK is used in many other applications and projects, in terms of PHP-GTK, it's an advanced set of classes which can be referenced to create widgets that we can then manipulate programmatically. PHP-GTK's programming style is similar to event driven programming languages such as Visual Basic and C++, in that it fires off signals. These signals can be captured and specific functions (called callback functions) can be defined to handle them.

I know that we haven't even looked at PHP-GTK yet, but consider the following PHP code:

$button = &new GtkButton("Click me");

$button->connect("clicked", "buttonClicked");

In the code above I've instantiated a new GtkButton class. I've then called its connect function passing in two parameters: the name of the signal to capture (clicked) as well as the name of a call-back function to execute when this signal is caught (buttonClicked). This tells PHP-GTK that when the button emits a "clicked" signal, the buttonClicked function should be called.

So if we added a buttonClicked function like this:

function buttonClicked()
{
echo "You clicked the button";
}

Then "You clicked the button" would be echoed to the screen when the button was clicked on.

That brings me to another point: PHP-GTK apps are run by calling them up with the normal PHP interpreter. The console window in which you called the PHP-GTK GUI app sits in the background and the app sits on top. While the app is running you can still output to the console window using echo, print, etc.

In my opinion this is excellent, because it means you can set a debug flag and treat the console window as a debug window, outputting debugging statements while you build your PHP GTK GUI app. When you're ready to distribute the app, simply set the debug flag to false or remove the echo commands.

Hopefully now you have a bit of an understanding of what PHP-GTK is. The next step is to actually download and install PHP-GTK, so let's go!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links