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

引用的解释

时间:2011-05-16 15:55:22  来源:站内  作者:潘春会
++;
echo
"increment $a->ref->id++;<br/><br/>";

print_r($a);
echo
"<br/>";
print_r($b);
echo
"<br/><br/>";
echo
"$a2=unserialize(serialize($a));<br/><br/>";
$a2=unserialize(serialize($a));

print_r($a2);
echo
"<br/>";
print_r($b);
echo
"<br/><br/>";

$a2->ref->id++;
echo
"increment $a2->ref->id++;<br/><br/>";

print_r($a2);
echo
"<br/>";
print_r($b);
echo
"<br/>";
?>

Output on PHP 5.3.3:
foo Object ( [ref] => foo Object ( [ref] => [id] => 1 ) [id] => 0 )
foo Object ( [ref] => [id] => 1 )

increment $a->ref->id++;

foo Object ( [ref] => foo Object ( [ref] => [id] => 2 ) [id] => 0 )
foo Object ( [ref] => [id] => 2 )

$a2=unserialize(serialize($a));

foo Object ( [ref] => foo Object ( [ref] => [id] => 2 ) [id] => 0 )
foo Object ( [ref] => [id] => 2 )

increment $a2->ref->id++;

foo Object ( [ref] => foo Object ( [ref] => [id] => 3 ) [id] => 0 )
foo Object ( [ref] => [id] => 2 )

after unserialize $a2->ref isn't any more a reference.

To save items for the next request, you must avoid references and save all objects of a class in an array. To get a PHP-reference you could write a function like:
<?php
function get_obj_by_ref($ref){
    global
$_array_where_objects_of_class_foo_are_stored;
   
$r = &$_array_where_objects_of_class_foo_are_stored[$ref];
    return
$r;
}
?>

I am unhappy about this behavior of PHP. I does not watch like a feature ... ;)

gnuffo1 at gmail dot com (28-Aug-2010 10:07)

 

 

 

If you want to know the reference count of a particular variable, then here's a function that makes use of debug_zval_dump() to do so:

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