免费邮箱 |加入收藏 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > PHP专区 > PHP技巧

引用的解释

时间:2011-05-16 15:55:22  来源:站内  作者:潘春会
info = $newVal;
    }
 }

 
// create data object from the A
 
$A_obj = new A();
 
// print the info property
 
echo 'A_obj info: ' . $A_obj->info . '<br/>';
 
 
// create the B object and pass the A_obj we created above
 
$B_obj = new B_class($A_obj);
 
// print the info property through the B object to make sure it has the same value 'eeee'
 
echo 'B_obj info: ' . $B_obj->A_obj->info . '<br/>';
 
 
// chage the info property
 
$B_obj->change('xxxxx');
 
// print the info property through the B object to make sure it changed the value to 'xxxxxx'
 
echo 'B_obj info after change: ' . $B_obj->A_obj->info . '<br/>';
 
// print the info property from the A_obj to see if the change through B_obj has affected it
 
echo 'A_obj info: ' . $A_obj->info . '<br/>';

?>

The result:

A_obj info: eeee
B_obj info: eeee
B_obj info after change: xxxxx
A_obj info: xxxxx

strata_ranger at hotmail dot com (01-Oct-2009 02:59)

 

 

 

Note that in PHP5 you generally don't need the reference operator -- at all -- when dealing with class objects, because PHP5 implements objects using Instances (which are more like C pointers than PHP's references system).

For example:
<?php
//
// Since PHP 5
//

$foo = new stdClass();

// $bar and $foo are still holding the same (single) object
$bar = $foo;
var_dump($foo, $bar)
// object(stdClass)#1 (0) { }
// object(stdClass)#1 (0) { }

$bar->member = "Something";
var_dump($foo
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门