【php类与对象】对象和引用
程序员文章站
2022-03-15 09:11:27
...
这篇文章介绍的内容是关于【php类与对象】对象和引用,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
对象和引用
PHP 的引用是别名,就是两个不同的变量名字指向相同的内容。
在 PHP 5,一个对象变量已经不再保存整个对象的值。只是保存一个标识符来访问真正的对象内容。 当对象作为参数传递,作为结果返回,或者赋值给另外一个变量,另外一个变量跟原来的不是引用的关系,只是他们都保存着同一个标识符的拷贝,这个标识符指向同一个对象的真正内容。
Example #1 引用和对象<?phpclass A { public $foo = 1; } $a = new A;$b = $a; // $a ,$b都是同一个标识符的拷贝 // ($a) = ($b) = <id>$b->foo = 2;echo $a->foo."\n";$c = new A;$d = &$c; // $c ,$d是引用 // ($c,$d) = <id>$d->foo = 2;echo $c->foo."\n";$e = new A;function foo($obj) { // ($obj) = ($e) = <id> $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. } ?>
相关推荐:
以上就是【php类与对象】对象和引用的详细内容,更多请关注其它相关文章!
上一篇: 微信小程序联网请求的轮播图
推荐阅读
-
PHP实现数组和对象的相互转换操作示例
-
Python面向对象编程中关于类和方法的学习笔记
-
透彻理解Java中Synchronized(对象锁)和Static Synchronized(类锁)的区别
-
PHP面向对象教程之自定义类
-
PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
-
PHP 面向对象程序设计(oop)学习笔记 (四) - 异常处理类Exception
-
PHP 面向对象程序设计(oop)学习笔记(三) - 单例模式和工厂模式
-
PHP 面向对象程序设计(oop)学习笔记 (二) - 静态变量的属性和方法及延迟绑定
-
ASP.NET自带对象JSON字符串与实体类的转换
-
Java中类、对象、封装、继承和多态(详细阐述)