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

PHP object-oriented - clone keyword

WBOY
Release: 2016-07-29 08:56:08
Original
847 people have browsed it

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>';
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template