<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> <br> 2</span> //<span style="color: #000000;"><span style="color: #000000;">深入理解PHP对象赋值</span> </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">echo</span> '<pre class="brush:php;toolbar:false">'<span style="color: #000000;">; </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> StdClass; </span><span style="color: #008080;"> 6</span> <span style="color: #800080;">$obj</span>->name = 'Pig'<span style="color: #000000;">; </span><span style="color: #008080;"> 7</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=> string(3) "Pig" }</span> <span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #800080;">$copy</span> = <span style="color: #800080;">$obj</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> $obj ,$copy都是new StdClass返回的同一个标识符的拷贝</span> <span style="color: #008080;">10</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$copy</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>string(3) "Pig" }</span> <span style="color: #008080;">11</span> <span style="color: #008080;">12</span> <span style="color: #800080;">$objRef</span> = &<span style="color: #800080;">$obj</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 此时会将$obj转换成引用,然后赋值给$objRef,因此$obj,$objRef都为引用 </span> <span style="color: #008080;">13</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$objRef</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>string(3) "Pig" }</span> <span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #800080;">$objClone</span> = <span style="color: #0000ff;">clone</span> <span style="color: #800080;">$obj</span>; <span style="color: #008000;">//</span><span style="color: #008000;">新空间</span> <span style="color: #008080;">16</span> <span style="color: #800080;">$obj</span>->name = 'After Clone'<span style="color: #000000;">; </span><span style="color: #008080;">17</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$objClone</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>string(3) "Pig" }</span> <span style="color: #008080;">18</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>string(11) "After Clone" } </span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span> <span style="color: #008000;">//unset是删除引用效果</span> <span style="color: #008080;">21</span> <span style="color: #800080;">$nameRef</span> = &<span style="color: #800080;">$obj</span>->name; <span style="color: #008000;">//</span><span style="color: #008000;">$obj->name被转换成引用(& string),然后赋给$nameRef</span> <span style="color: #008080;">22</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#2 (1) { ["name"]=>&string(11) "After Clone" }</span> <span style="color: #008080;">23</span> <span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$nameRef</span>); <span style="color: #008000;">//</span><span style="color: #008000;">删除引用</span> <span style="color: #008080;">24</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>string(11) "After Clone" } </span><span style="color: #008080;">25</span> <span style="color: #008080;">26</span> <span style="color: #008000;">//null是赋值效果 </span> <span style="color: #008080;">27</span> <span style="color: #800080;">$nameRef</span> = &<span style="color: #800080;">$obj</span>->name; <span style="color: #008000;">//</span><span style="color: #008000;">恢复name的引用</span> <span style="color: #008080;">28</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#2 (1) { ["name"]=>&string(11) "After Clone" }</span> <span style="color: #008080;">29</span> <span style="color: #800080;">$nameRef</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">; </span><span style="color: #008080;">30</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#2 (1) { ["name"]=>&NULL }</span> <span style="color: #008080;">31</span> <span style="color: #008080;">32</span> <span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$objRef</span>); <span style="color: #008000;">//</span><span style="color: #008000;">仅仅删除了引用</span> <span style="color: #008080;">33</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>&NULL }</span> <span style="color: #008080;">34</span> <span style="color: #008080;">35</span> <span style="color: #800080;">$objRef</span> = &<span style="color: #800080;">$obj</span>; <span style="color: #008000;">//</span><span style="color: #008000;">恢复对象引用</span> <span style="color: #008080;">36</span> <span style="color: #800080;">$obj</span>->name = 'Lucy'<span style="color: #000000;">; </span><span style="color: #008080;">37</span> <span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">null</span>; <span style="color: #008000;">//</span><span style="color: #008000;">赋值$obj为null,$obj只是new StdClass的标识拷贝,不会影响其内容。 </span><span style="color: #008080;">38</span> <span style="color: #008000;"> //$objRef做为$obj的引用,会同时被赋值null </span><span style="color: #008080;">39</span> <span style="color: #008000;"> //等价 $objRef = null;</span> <span style="color: #008080;">40</span> <span style="color: #008080;">41</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>,<span style="color: #800080;">$copy</span>,<span style="color: #800080;">$objRef</span>,<span style="color: #800080;">$objClone</span><span style="color: #000000;">); </span><span style="color: #008080;">42</span> <span style="color: #008000;">//</span><span style="color: #008000;"> NULL, </span><span style="color: #008080;">43</span> <span style="color: #008000;">// object(stdClass)#1 (1) { ["name"]=>&string(4) "Lucy" }, </span><span style="color: #008080;">44</span> <span style="color: #008000;">// NULL, </span><span style="color: #008080;">45</span> <span style="color: #008000;">// object(stdClass)#1 (1) { ["name"]=>string(3) "Pig" }</span> <span style="color: #008080;">46</span> <span style="color: #008080;">47</span> ?>