Gotcha: PHP-GTK connect_simple sends no children when ‘add’ signal is emitted
I have been learning PHP-GTK. I was following Chapter 4 of the Pro PHP-GTK book by Scott Mattocks, published by Apress (published 2006).
When I came to experiment with Listing 4-4 I discovered that the code performed in an unexpected way. Amongst the error messages that were thrown I got something along the lines of:
warning: missing argument 1 for extendedcontainer::checkLimit()
...
On page 45 of the book it says “If you create a signal handler for the add signal using connect , the callback method must accept at least two parameters: the container and the child. If you create a similar signal handler with connect_simple, the callback needs to accept only the child as an argument”.
This does not appear to be true. If you create a similar signal handler with connect_simple, the callback actually receives no arguments.
I am not sure whether this is a bug, a typo in the book or just a consequence of the different versions of PHP-GTK.
Anyway this piece of code should explain what happens when you try either connect_simple() or connect() to attach a callback to the ‘add’ signal of a container
<?php//connect_test.php
function handleAddSignal($arg1=null, $arg2=null) {
echo "arg1 is a ". gettype($arg1);if (gettype($arg1) == 'object') echo " of class " . get_class($arg1);
echo "n";
echo "arg2 is a ". gettype($arg2);
if (gettype($arg2) == 'object') echo " of class " . get_class($arg2);
echo "n";
}
echo "php version: " .phpversion() ." n";
echo "gtk version: " .gtk::get_version() ." n";
$containerA = new GtkHBox();
$containerB = new GtkHBox();
$buttonA = new GtkButton();
$buttonB = new GtkButton();
echo "connecting 'add' signal to container A using connect_simple() n";
$containerA->connect_simple('add', 'handleAddSignal');
echo "adding an object to container A n";
$containerA->add($buttonB);
echo "connecting 'add' signal to container B using connect() n";
$containerB->connect('add', 'handleAddSignal');
echo "adding an object to container B n";
$containerB->add($buttonA);
When running this script from the Windows XP Command Prompt, i got the following output:
# php connect_test.php php version: 5.2.9 gtk version: GTK 2.12.9 connecting 'add' signal to container A using connect_simple() adding an object to container A arg1 is a NULL arg2 is a NULL connecting 'add' signal to container B using connect() adding an object to container B arg1 is a object of class GtkHBox arg2 is a object of class GtkButton
So there is something strange going on.
