zoukankan      html  css  js  c++  java
  • PHP深浅拷贝

    深拷贝:赋值时值完全复制,完全的copy,对其中一个作出改变,不会影响另一个.   
    浅拷贝:赋值时,引用赋值,相当于取了一个别名。对其中一个修改,会影响另一个.
    

    1.普通变量赋值为深拷贝

    <?php
    $a='me';
    $b=$a;
    echo ('a: '.$a.' b:'.$b);
    $b='you';
    echo "
    ";
    echo ('a: '.$a.' b:'.$b);
    ?>
    

    结果

    a: me b:me
    a: me b:you
    

    2.普通变量的引用赋值为浅拷贝

    <?php
    $a='me';
    $b=&$a;
    echo ('a: '.$a.' b:'.$b);
    $b='you';
    echo "
    ";
    echo ('a: '.$a.' b:'.$b);
    ?>
    

    结果

    a: me b:me
    a: you b:you
    

    3.对象的赋值为浅拷贝

    <?php
    class top{
        public $file;
    }
    $a=new top();
    $a->file='flag.php';
    $b=$a;
    echo ('a:'.$a->file.' b: '.$b->file);
    echo "
    ";
    $b->file='index.php';
    echo ('a:'.$a->file.' b: '.$b->file);
    ?>
    

    结果

    a:flag.php b: flag.php
    a:index.php b: index.php
    

    4.对象的clone为深拷贝

    <?php
    class top{
        public $file;
    }
    $a=new top();
    $a->file='flag.php';
    $b=clone $a;
    echo ('a:'.$a->file.' b: '.$b->file);
    echo "
    ";
    $b->file='index.php';
    echo ('a:'.$a->file.' b: '.$b->file);
    ?>
    

    结果

    a:flag.php b: flag.php
    a:flag.php b: index.php
    
  • 相关阅读:
    8月4日
    8月3日 hive配置
    8月2日
    8月1日
    7月31日
    7月30日
    7月29日
    7月28日
    第六周总结
    重大技术需求进度报告一
  • 原文地址:https://www.cnblogs.com/LLeaves/p/12888407.html
Copyright © 2011-2022 走看看