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

引用的解释

时间:2011-05-16 15:55:22  来源:站内  作者:潘春会
)
{
   
ob_start();
   
debug_zval_dump($var);
   
$dump = ob_get_clean();

   
$matches = array();
   
preg_match('/refcount(([0-9]+)/', $dump, $matches);

   
$count = $matches[1];

   
//3 references are added, including when calling debug_zval_dump()
   
return $count - 3;
}
?>

debug_zval_dump() is a confusing function, as explained in its documentation, as among other things, it adds a reference count when being called as there is a reference within the function. refcount() takes account of these extra references by subtracting them for the return value.

It's also even more confusing when dealing with variables that have been assigned by reference (=&), either on the right or left side of the assignment, so for that reason, the above function doesn't really work for those sorts of variables. I'd use it more on object instances.

However, even taking into account that passing a variable to a function adds one to the reference count; which should mean that calling refcount() adds one, and then calling debug_zval_dump() adds another, refcount() seems to have aquired another reference from somewhere; hence subtracting 3 instead of 2 in the return line. Not quite sure where that comes from.

I've only tested this on 5.3; due to the nature of debug_zval_dump(), the results may be completely different on other versions.

Youssef Omar (16-Dec-2009 04:27)

 

 

 

This is to show the affect of changing property of object A through another object B when you pass object A as a property of another object B.

<?php
 
// data class to be passed to another class as an object
 
class A{
     public
$info;
    function
__construct(){
       
$this->info = "eeee";
    }
 }
 
 
// B class to change the info in A obj
 
class B_class{
     public
$A_obj;
   
    function
__construct($A_obj){
       
$this->A_obj $A_obj;
    }
    public function
change($newVal){
       
$this->A_obj->
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门