Object References (the Ampersand &)
Support for object orientation was completely new to PHP 4, and was therefore
not very sophisticated in comparison to the OO support in other languages, such
as Java. When assigning variables, it was absolutely necessary not to make copies
of GTK objects, but to pass references.
//PHP 4: copy as default behavior
$a = new GtkLabel();
$a->set_text('1');
$b = $a;
$b->set_text('2');
echo $a->get();//still 1
|
So to not make a copy, you had to use the ampersand & when assigning variables:
//PHP 4: making references
$a = new GtkLabel();
$a->set_text('1');
$b = &$a;
$b->set_text('2');
echo $a->get();//is 2 now
|
However, a copy of the object
still was made: on construction.
To be totally correct, especially with GTK widgets, you had to do:
//PHP 4: reference on instantiation
$a = &new GtkLabel();
|
With PHP 5, things have changed: pass-by-reference is the default behavior now - one
doesn't need the ampersand any more! The following script works under PHP 5 with
PHP-GTK 2, without any problems:
<?php
//PHP5: no Ampersand any more
$a = new GtkLabel();
$a->set_text('1');
$b = $a;
$b->set_text('2');
echo $a->get_text();//is 2
?> |
The same applies for callbacks: no ampersand any more!
Whereas you had to do the following under PHP 4 and GTK 1:
$window->connect_object('destroy', array(&$object, 'function'));
|
you simply leave out the & with PHP 5 and GTK 2:
$window->connect_simple('destroy', array($object, 'function'));
|