21,886
社区成员
发帖
与我相关
我的任务
分享
$t = new T();
$s = $t;
$n = &$t;//这里会导致Separation,$s会被存到新的zval 所以$t = null不会影响$s
class a {
public $a;
public function __construct($val) {
$this->a = $val;
}
}
$objs = array();
$obj = null;
for($i = 0; $i < 5;$i++) {
$obj = &new a($i); //下面输出01234
//$obj = new a($i); //下面输出44444
$objs[] = &$obj;
}
for($i = 0; $i < 5;$i++) {
echo $objs[$i]->a;
}
class a
{
public $a = 1;
function out()
{
$this->a ++;
}
}
function getVar($obj)
{
$obj->out();
}
$a = & new a();
$b = new a();
getVar($a);
getVar($b);
debug_zval_dump($a);
debug_zval_dump($b);
class T{}
$t = new T();
$t2 = & new T();
$t3 = new T();
echo "<pre/>";
//refcount显示为2,其内部refcount应为1,因为debug_zval_dump会使变量有个引用过程。
debug_zval_dump($t);
debug_zval_dump($t2);//发现refcount比其它的同类少了一次。说明内部实现有些许不同,但是php5已经不推荐用&返回对象了。
debug_zval_dump($t3);
class T{}
$t = new T();
$s = $t;
$n = &$t;
$t->str = '性别男,爱好女';
$t = null;//不该用unset
echo $n->str;//nothing...
echo $s->str;//php5+,大家猜猜输出了什么?,或许能对本楼有点贡献
class a
{
public $a = 1;
function __construct()
{
$this->a = 0;
}
function out()
{
$this->a ++;
}
}
function getVar($obj)
{
$obj->out();
return $obj->a;
}
$b = new a();
$a = & new a();
//函数调用时,函数内部会重新拷贝类a的实例
echo getVar($b);
//如果是引用的话,函数内部就不会重新拷贝类a的实例。
echo getVar($a);