Hello World Tutorial

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.

These first few lines check to see if the PHP-GTK extension is already available, and loads it if it isn't. This is done by the dl('php_gtk.dll'); or dl('php_gtk.so'); statements on Windows and Linux respectively. The PHP_SHLIB_SUFFIX takes care of the specific extensions used by Windows and Linux.

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.

The shutdown() function is registered as a handler for the "destroy" signal. The function prints the text "Shutting down...\n" to the console and then calls the static function gtk::main_quit() .

The hello() function is registered as a handler for the "clicked" signal on the button. It globalises the $window variable so it can access the instance of GtkWindow created further down the script. It then prints the text "Hello World" to the console before calling the destroy() method on the window, which in turn fires the "destroy" signal, which in turn calls the shutdown() function.

Another way that the hello() function would be able to access the $window variable is if the variable were passed as a custom parameter.

The next four lines set up the window itself. Firstly we create an instance of GtkWindow. Once this has been done successfully, we call the connect() method from the window in order to register the shutdown() function as the handler for the "destroy" signal and the delete_event() function as the handler for the "delete-event" signal. Finally, we call the set_border_width() function to set a 10-pixel wide border on the instance of GtkWindow that we just created.

These three lines of the script create and set up the button. In the first line of the above code snippet we create a new instance of the GtkButton widget. The argument to the constructor is the text we want the button to display - in this case "Hello World!". We then call the connect() method to register the hello() function we defined earlier, as the handler for the "clicked" signal. Finally we add the button to the window we previously created by calling the GtkContainer method add() from our containing $window, and then display everything contained by $window (and its child widget, $button) by calling the show_all() method, also from our instance of GtkWindow.

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.

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.