Home >Backend Development >PHP Tutorial >PHP object-oriented - clone keyword

PHP object-oriented - clone keyword

WBOY
WBOYOriginal
2016-07-29 08:56:08850browse

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 = &#39;&#39;;
}

$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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn