The clone keyword is used to copy an object. The copied object remains independent from the source object. Modifying its properties will not affect the source object. However, direct assignment of references is different. It will cause the modifications to also affect the source object. For example:
<?php class NbaPlayer{ public $name = ''; } $james = new NbaPlayer(); $james->name = 'james'; echo 'james`s name is '.$james->name.'<br>'; $james2 = clone $james;//克隆 $james2->name = 'james2'; echo 'after assign :<br>'; echo 'james`s name is '.$james->name.'<br>'; echo 'james2`s name is '.$james2->name.'<br>'; $james3 = $james; //赋值 $james3->name = 'james3'; echo 'after assign:<br>'; echo 'james`s name is '.$james->name.'<br>'; echo 'james2`s name is '.$james3->name.'<br>';
Result after running:
james`s name is james
after clone :
james`s name is james
james2`s name is james2
after assign:
james`s name is james3
james2`s name is james3
The above has introduced the PHP object-oriented clone keyword, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.