当前位置:Gxlcms > PHP教程 > 【php类与对象】对象和引用

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

时间:2021-07-01 10:21:17 帮助过:5人阅读

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

对象和引用

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

  1. Example #1 引用和对象<?phpclass A {
  2. public $foo = 1;
  3. }
  4. $a = new A;$b = $a; // $a ,$b都是同一个标识符的拷贝
  5. // ($a) = ($b) = <id>$b->foo = 2;echo $a->foo."\n";$c = new A;$d = &$c; // $c ,$d是引用
  6. // ($c,$d) = <id>$d->foo = 2;echo $c->foo."\n";$e = new A;function foo($obj) {
  7. // ($obj) = ($e) = <id>
  8. $obj->foo = 2;
  9. }
  10. foo($e);echo $e->foo."\n";?>

User Contributed Notes

  1. /*
  2. Notes on reference:
  3. A reference is not a pointer. However, an object handle IS a pointer. Example:
  4. */<?phpclass Foo {
  5. private static $used; private $id; public function __construct() {
  6. $id = $used++;
  7. } public function __clone() {
  8. $id = $used++;
  9. }
  10. }$a = new Foo;
  11. // $a is a pointer pointing to Foo object 0$b = $a;
  12. // $b is a pointer pointing to Foo object 0, however, $b is a copy of $a$c = &$a;
  13. // $c and $a are now references of a pointer pointing to Foo object 0$a = new Foo;
  14. // $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);
  15. // A reference with reference count 1 is automatically converted back to a value. Now $c is a pointer to Foo object 1$a = &$b;
  16. // $a and $b are now references of a pointer pointing to Foo object 0$a = NULL;
  17. // $a and $b now become a reference to NULL. Foo object 0 can be garbage collected nowunset($b);
  18. // $b no longer exists and $a is now NULL$a = clone $c;
  19. // $a is now a pointer to Foo object 2, $c remains a pointer to Foo object 1unset($c);
  20. // Foo object 1 can be garbage collected now.$c = $a;
  21. // $c and $a are pointers pointing to Foo object 2unset($a);
  22. // Foo object 2 is still pointed by $c$a = &$c;
  23. // Foo object 2 has 1 pointers pointing to it only, that pointer has 2 references: $a and $c;const ABC = TRUE;
  24. if(ABC) {
  25. $a = NULL;
  26. // Foo object 2 can be garbage collected now because $a and $c are now a reference to the same NULL value
  27. } else {
  28. unset($a);
  29. // Foo object 2 is still pointed to $c
  30. }

  1. <?php
  2. //The two are meant to be the same$a = "Clark Kent";
  3. //a==Clark Kent$b = &$a;
  4. //The two will now share the same fate.$b="Superman";
  5. // $a=="Superman" too.echo $a;
  6. echo $a="Clark Kent";
  7. // $b=="Clark Kent" too.unset($b);
  8. // $b porced from $a$b="Bizarro";
  9. echo $a;
  10. // $a=="Clark Kent" still, since $b is a free agent pointer now.
  11. //The two are NOT meant to be the same.$c="King";
  12. $d="Pretender to the Throne";echo $c."\n";
  13. // $c=="King"echo $d."\n";
  14. // $d=="Pretender to the Throne"swapByValue($c, $d);
  15. echo $c."\n";
  16. // $c=="King"echo $d."\n";
  17. // $d=="Pretender to the Throne"swapByRef($c, $d);
  18. echo $c."\n";
  19. // $c=="Pretender to the Throne"echo $d."\n";
  20. // $d=="King"function swapByValue($x, $y)
  21. {
  22. $temp=$x;$x=$y;$y=$temp;
  23. //All this beautiful work will disappear
  24. //because it was done on COPIES of pointers.
  25. //The originals pointers still point as they did.}function swapByRef(&$x, &$y)
  26. {
  27. $temp=$x;$x=$y;
  28. $y=$temp;
  29. //Note the parameter list: now we switched 'em REAL good.
  30. }
  31. ?>

相关推荐:

【php类与对象】trait

【php类与对象】Final 关键字

【php类与对象】后期静态绑定

以上就是【php类与对象】对象和引用的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行