When you begin to learn a programming language, the first program you often write is a 'hello world' program. So, just to fit in with everyone else, the first tutorial in this manual just happens to be a 'hello world' tutorial!
Throughout the tutorials we expect a reasonable grasp of PHP itself. The tutorials are designed to give the user an idea of how to use PHP-GTK, and the ideas and techniques behind it.
In this tutorial we will create a simple window with the text "Hello World!" in it.
We will start by listing the program and will then explain each line of the program, giving an overview of a very basic PHP-GTK application.
Example 1.1. PHP-GTK Hello World Program Listing
<?php
if( !extension_loaded('gtk')) {
dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
}
function delete_event()
{
return false;
}
function shutdown()
{
print("Shutting down...\n");
gtk::main_quit();
}
function hello()
{
global $window;
print "Hello World!\n";
$window->destroy();
}
$window = &new GtkWindow();
$window->connect('destroy', 'shutdown');
$window->connect('delete-event', 'delete_event');
$window->set_border_width(10);
$button = &new GtkButton('Hello World!');
$button->connect('clicked', 'hello');
$window->add($button);
$window->show_all();
gtk::main();
?>
|
<?php
if( !extension_loaded('gtk')) {
dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
}
?>
|
The delete_event() function is registered as a handler (see below) for the "delete-event" signal. It returns false, telling PHP-GTK to fire the event's default signal handler, which in this case is the destroy() method. If the function were to return true, PHP-GTK would stop the default signal handler running at this point. This is useful to know if you need to write a user-defined function in place of destroy() - for example, to produce a dialog box confirming that the user intends to close down the application.
It is not strictly necessary to return false in order to connect the "delete-event" signal to the destroy() method, as this particular signal returns false by default. It is possible to not specify any behaviour at all for a window's "delete-event" signal, just so long as the "destroy" signal is handled in the correct way, as it is here.
Example 1.4. The shutdown() function
<?php
function shutdown()
{
print("Shutting down...\n");
gtk::main_quit();
}
?>
|
Example 1.5. The hello() function
<?php
function hello()
{
global $window;
print "Hello World!\n";
$window->destroy();
}
?>
|
Another way that the hello() function would be able to access the $window variable is if the variable were passed as a custom parameter.
Example 1.6. Setting up the Window
<?php
$window = &new GtkWindow();
$window->connect('destroy', 'shutdown');
$window->connect('delete-event', 'delete_event');
$window->set_border_width(10);
?>
|
Example 1.7. Setting up the Button
<?php
$button = &new GtkButton('Hello World!');
$button->connect('clicked', 'hello');
$window->add($button);
$window->show_all();
?>
|
The final line of the script calls the static gtk::main function. This tells PHP-GTK that we have finished setting up our interface, and that the main loop can begin listening for the events fired by user interaction so that the callback functions we defined earlier can be called and the various actions carried out.