Article
Build Cross-Platform Windowed Apps with PHP
Run reverse.php. When you click on the button, the text in the text box will be reversed, like this:

It's time to introduce a new concept regarding PHP-GTK. Put simply, PHP-GTK doesn't support absolute positioning of widgets, which means that we can't add a text box 10 pixels from the left of a window and 20 pixels down. PHP-GTK only supports relative positioning, which requires us to define areas into which widgets can be placed.
The GtkHBox and GtkVBox classes create horizontal and vertical boxes that act as containers for other widgets. In our example above, we create a new GtkVBox object, adding it to our window, like this:
// Add a GtkVBox class to our window
$box = &new GtkVBox();
$window->add($box);
All GTK widgets that are containers (i.e. can hold other widgets) expose the add function, which accepts one parameter: a widget. This widget is then displayed within that container.
The GtkVBox widget inherits many of its functions from its base classes. All GTK widgets are derived from base classes, so most of the widgets share similar functionality. Here's the hierarchy of the GtkVBox class:
GtkObject
|-- GtkWidget
|---- GtkContainer
|------ GtkBox
|-------- GtkVBox
The GtkBox class exposes a pack_start function. This function is used to add a widget to a GtkBox class or derived class. In our example, we create a new GtkEntry and GtkButton class, adding them to our GtkVBox object using its pack_start function, like this:
// Add a GtkEntry class to our window
$entry = &new GtkEntry();
$entry->set_text("Hello World");
$box->pack_start($entry);
// Add a GtkButton class to our window
$button = &new GtkButton("Reverse Text");
$button->connect("clicked", "reverseText", $entry);
$box->pack_start($button);
As you can see in the example above, we register a callback function for the "clicked" signal of our button. Remember back to our first window.php example where our call to connect contained only two arguments. In the example above, we specify a third parameter, which is our GtkEntry widget, $entry. The callback function we specify is reverseText, which looks like this:
function reverseText($theButton, $theEntry)
{
$text = strrev($theEntry->get_text());
$theEntry->set_text($text);
}
The reverseText function accepts two arguments: the widget that emitted the signal (which in our case is the button) as well as a custom argument (which is our GtkEntry widget). The first argument, $theButton is passed in implicitly by PHP-GTK, and we have manually passed in the second argument when we defined our callback function for the buttons "clicked" signal.
Our reverseText function gets the text from our GtkEntry object by calling its get_text function. This text is reversed using PHP's strrev function, and is then put back into our GtkEntry object by calling its set_text function.