php对象与引用,【php类与对象】对象和引用

本文探讨了PHP中对象与引用的概念,讲解了对象变量不再保存完整值,而是保存标识符的重要性。通过实例展示了引用和对象的区别,以及如何在函数传递中实现引用和复制的效果。同时揭示了PHP中对象和引用计数的底层机制。
摘要由CSDN通过智能技术生成

这篇文章介绍的内容是关于【php类与对象】对象和引用,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

对象和引用

PHP 的引用是别名,就是两个不同的变量名字指向相同的内容。

在 PHP 5,一个对象变量已经不再保存整个对象的值。只是保存一个标识符来访问真正的对象内容。 当对象作为参数传递,作为结果返回,或者赋值给另外一个变量,另外一个变量跟原来的不是引用的关系,只是他们都保存着同一个标识符的拷贝,这个标识符指向同一个对象的真正内容。Example #1 引用和对象<?phpclass A {

public $foo = 1;

}

$a = new A;$b = $a; // $a ,$b都是同一个标识符的拷贝

// ($a) = ($b) = $b->foo = 2;echo $a->foo."\n";$c = new A;$d = &$c; // $c ,$d是引用

// ($c,$d) = $d->foo = 2;echo $c->foo."\n";$e = new A;function foo($obj) {

// ($obj) = ($e) =

$obj->foo = 2;

}

foo($e);echo $e->foo."\n";?>

User Contributed Notes/*

Notes on reference:

A reference is not a pointer. However, an object handle IS a pointer. Example:

*/<?phpclass Foo {

private static $used; private $id; public function __construct() {

$id = $used++;

} public function __clone() {

$id = $used++;

}

}$a = new Foo;

// $a is a pointer pointing to Foo object 0$b = $a;

// $b is a pointer pointing to Foo object 0, however, $b is a copy of $a$c = &$a;

// $c and $a are now references of a pointer pointing to Foo object 0$a = new Foo;

// $a and $c are now references of a pointer pointing to Foo object 1, $b is still a pointer pointing to Foo object 0unset($a);

// A reference with reference count 1 is automatically converted back to a value. Now $c is a pointer to Foo object 1$a = &$b;

// $a and $b are now references of a pointer pointing to Foo object 0$a = NULL;

// $a and $b now become a reference to NULL. Foo object 0 can be garbage collected nowunset($b);

// $b no longer exists and $a is now NULL$a = clone $c;

// $a is now a pointer to Foo object 2, $c remains a pointer to Foo object 1unset($c);

// Foo object 1 can be garbage collected now.$c = $a;

// $c and $a are pointers pointing to Foo object 2unset($a);

// Foo object 2 is still pointed by $c$a = &$c;

// Foo object 2 has 1 pointers pointing to it only, that pointer has 2 references: $a and $c;const ABC = TRUE;

if(ABC) {

$a = NULL;

// Foo object 2 can be garbage collected now because $a and $c are now a reference to the same NULL value

} else {

unset($a);

// Foo object 2 is still pointed to $c

}<?php

//The two are meant to be the same$a = "Clark Kent";

//a==Clark Kent$b = &$a;

//The two will now share the same fate.$b="Superman";

// $a=="Superman" too.echo $a;

echo $a="Clark Kent";

// $b=="Clark Kent" too.unset($b);

// $b porced from $a$b="Bizarro";

echo $a;

// $a=="Clark Kent" still, since $b is a free agent pointer now.

//The two are NOT meant to be the same.$c="King";

$d="Pretender to the Throne";echo $c."\n";

// $c=="King"echo $d."\n";

// $d=="Pretender to the Throne"swapByValue($c, $d);

echo $c."\n";

// $c=="King"echo $d."\n";

// $d=="Pretender to the Throne"swapByRef($c, $d);

echo $c."\n";

// $c=="Pretender to the Throne"echo $d."\n";

// $d=="King"function swapByValue($x, $y)

{

$temp=$x;$x=$y;$y=$temp;

//All this beautiful work will disappear

//because it was done on COPIES of pointers.

//The originals pointers still point as they did.}function swapByRef(&$x, &$y)

{

$temp=$x;$x=$y;

$y=$temp;

//Note the parameter list: now we switched 'em REAL good.

}

?>

相关推荐:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值