Revisited: Building Cross Platform GUI Apps With PHP-GTK - Building our first PHP-GTK app
(Page 5 of 10 )
Now that PHP-GTK's installed, we're ready to create our first GUI application. Create a new PHP script called window.php and enter the following code into it:
<?php
// Load the PHP-GTK extension
$extension = "php_gtk" . (strpos(PHP_OS, "WIN") >= 0 ? ".dll" : ".so");
dl($extension);
// Create a new window
$window = &new GtkWindow();
$window->set_title("Test App");
$window->set_usize(300, 100);
$window->set_position(GTK_WIN_POS_CENTER);
$window->set_policy(false, false, false);
// Set a callback function for the destroy signal
$window->connect("destroy", "killwin");
function killwin()
{
echo("Window Destroyed!\n");
gtk::main_quit();
}
// Show the window
$window->show_all();
// Start the main PHP-GTK listener loop
gtk::main();
?>Change into the directory where you created window.php and use the following command to execute it:
php –q window.phpHere's the result of window.php on my Windows 2000 machine:

If you play around with the window, then you'll notice that you can't resize it and can't maximize it, because of the calls that window.php makes to various GTK functions. When you close the window, you'll notice that some text has been output to the console window:
C:\>php -q window.php
Window Destroyed!Let's now run through the code that we used to create window.php.
// Load the PHP-GTK extension
$extension = "php_gtk" . (strpos(PHP_OS, "WIN") >= 0 ? ".dll" : ".so");
dl($extension);As the comment indicates, we load the PHP-GTK extension. The PHP_OS variable contains a word that represents your operating system. On my machine, PHP_OS is "WINNT". We set the value of the $library variable to either php_gtk.dll (for Windows) or php_gtk.so (for Linux), depending on whether or not PHP_OS contains the string "WIN". We then call the dl() function to load the extension into memory.
// Create a new window
$window = &new GtkWindow();
$window->set_title("Test App");
$window->set_usize(300, 100);
$window->set_position(GTK_WIN_POS_CENTER);
$window->set_policy(false, false, false);The most important aspect of any application is the user interface. PHP-GTK defines a class called GtkWindow, which we can instantiate by reference to create a new window. The GtkWindow class includes several functions that we can use to specify how the window will look, where it will be located, etc. In our example i've set the windows title using set_title, set its width and height using set_usize, set its position to the center of the screen, and also used set_policy to tell PHP-GTK that the window can't be resized.
// Set a callback function for the destroy signal
$window->connect("destroy", "killwin");
function killwin()
{
echo("Window Destroyed!\n");
gtk::main_quit();
}Most PHP-GTK widgets include a method called connect(), which allows us to specify callback functions that should be executed when a specific even is captured. The PHP-GTK library monitors our application in much the same way as any other object-orientated, event driven programming language. PHP-GTK is based on the concept of signals and callbacks, meaning that if we move the mouse over a button for example, then a signal is emitted. We can register a callback function to tell PHP-GTK which function(s) to run when this specific signal is emitted.
In our example, I've used the connect function to tell PHP-GTK that when our window emits the "destroy" signal (when we close the window) that it should execute our killwin function. Our killwin function contains a call to the static function main_quit of the gtk class, which unloads our window and terminates our application.
// Show the window
$window->show_all();
// Start the main PHP-GTK listener loop
gtk::main();The show_all() function displays our window, and if it contained any widgets then these would be displayed as well. The main function of the gtk class is called statically, and tells the PHP-GTK library to begin its listener loop.
Next: Registering multiple callback functions >>
More PHP Articles
More By Mitchell Harper