Hello World (basic)

Table of Contents

Further reading

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 2.1. A simple Hello World script

<?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("Just wanted to say\r\n'Hello world!'");
$wnd->add($lblHello);

$wnd->show_all();
Gtk::main();
?>

If you get an error Fatal error: Call to undefined function: connect_simple(), then you are using PHP-Gtk1 and not PHP-Gtk2. This is the wrong manual then.

Now copy the code into a text editor and save it as hello.phpw. Then open a console and start it via php hello.phpw. A window with title "Hello world" should open and contain nothing but the text "Just wanted to say 'Hello world!'".

Example 2.2. Checking if PHP-GTK is available

if (!class_exists('gtk')) {
    die("Please load the php-gtk2 module in your php.ini\r\n");
}

Here we make sure that PHP-GTK is available by checking if the class gtk exists. Unlike PHP 4 and PHP-GTK 1, you should not try to load the PHP-GTK module via dl(). This has been deprecated in PHP 5, and should not be used in newly written code. So all we can do is print out a message, that the module is not available and should be enabled by the user.

Example 2.3. Create a window

$wnd = new GtkWindow();

Creating a new window widget is as easy as instantiating a new object: Variable $wnd gets the new object assigned. If you already programmed with PHP 4 and PHP-GTK 1, you will miss the ampersand & before new. That was required in PHP 4, but may not be used any more in PHP 5, as references are created automatically.

Example 2.4. Set the window title

$wnd->set_title('Hello world');

To make it easier to identify the window in the task bar, we set the title: Just a normal call to an object method.

Example 2.5. Enable a clean shutdown

$wnd->connect_simple('destroy', array('gtk', 'main_quit'));

That is the first interesting piece of code: The "destroy" signal gets connected to the static Gtk::main_quit method. It basically tells GTK to quit the main loop when the window gets closed/destroyed. (The main loop is explained some paragraphs below.)

Example 2.6. Create a label to display text

$lblHello = new GtkLabel("Just wanted to say\r\n'Hello world!'");

As done before, we create a new widget. This time we want to display a small amount of text, and GtkLabel is perfect at this task.

Example 2.7. Add the label to the window

$wnd->add($lblHello);

Now we tell the window, that the label shall be added directly on it.

Example 2.8. Make the window visible

$wnd->show_all();

Until now, you will see nothing on your screen. To make the window visible, you will need to call the show() . That would make the window visible, but not the label - we would have to call show() on the label, too. That is inconvenient if you have multiple widgets like buttons, checkboxes and other on the window: show_all() takes care of making the window and all child widgets visible.

Example 2.9. Start the main loop

Gtk::main();

After everything is set up, we need to make sure that the window stays open and responds to user interactions. A normal PHP script would end and stop at this point, but we want to keep it running: For that, the GTK main loop needs to be started. It takes care of keeping the application running and waiting for user events. If events occur, it delegates them to the corresponding callbacks.

Further reading

Now that you have created your first PHP-GTK 2 application, you probably want to create more complex layouts. To lean the basics, have a look at the Packing tutorial.