PHP Object Assignment vs Cloning

前端 未结 7 1268
后悔当初
后悔当初 2020-11-29 05:34

I know this is covered in the php docs but I got confused with this issue .

From the php docs :

$instance = new SimpleClass();
$assigned   =  $ins         


        
7条回答
  •  时光说笑
    2020-11-29 05:54

    It just makes a completely new object, which retains the properties of the object being copied. It performs a deep copy:

    $assigned = $instance; 
    

    It makes a shallow copy when you copy an object from one to another:

    $assigned = clone $instance;
    

提交回复
热议问题