Object references (the Ampersand &)

Source: Internet
Author: User
Tags gtk

Support for object orientation was completely new to PHP 4, and was therefore not very sophisticated in comparison to the OO support in other versions ages, 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 StillWas 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‘));

Object references (the Ampersand &)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.