Change into the directory where you created window.php and use the following command to execute it:
php –q window.php
Here'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
Let's now run through the code that we used to create window.php.
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_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
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
// Start the main PHP-GTK listener loop
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.