码迷,mamicode.com
首页 > 其他好文 > 详细

Object References (the Ampersand &)

时间:2014-11-01 09:40:53      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:des   io   ar   java   for   sp   div   on   cti   

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‘));

Object References (the Ampersand &)

标签:des   io   ar   java   for   sp   div   on   cti   

原文地址:http://www.cnblogs.com/huaziking/p/4066379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!