• 技术文章 >后端开发 >php教程

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

    不言不言2018-04-18 10:25:01原创1958

    这篇文章介绍的内容是关于【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类与对象】trait

    【php类与对象】Final 关键字

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

    php入门到就业线上直播课:查看学习

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

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:php 引用 对象
    上一篇:PHP获取多维数组指定的值在数组第几列 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• PHP底层的运行机制与原理_PHP教程• 说一说PHP程序开发中的模板选择_PHP• 程序修改有关问题• js表单提交解决方法• php 适用方法积累
    1/1

    PHP中文网