Home > Backend Development > PHP Tutorial > Learn about PHP object cloning clone

Learn about PHP object cloning clone

jacklove
Release: 2023-03-31 10:18:01
Original
1772 people have browsed it

php Object copying is to copy the reference address of the object, so when using $objA = $objB, $objA and $objB will point to the same memory address. When $objA changes, $objB is also affected.
If we want the $objA object to be copied into an $objB object, any changes to $objA after copying will not affect $objB. That is, $objA and $objB are two independent objects, but the initial value of $objB is created by $objA. A more efficient way is to use the clone() method.
$objB = clone $objA;
The value of $objB is the instance changed by the clone() method in the $objA instance object based on $objA.
When the object is copied, the references in all attributes remain unchanged and point to the original variables. If the __clone() method is defined, the __clone( ) method will be called and can be used to modify the value of the attribute.

Example 1: clone object

<?php
class subclass{
	private $name;
	private $age;
	public function __construct(){
		$this->name = &#39;fdipzone&#39;;
		$this->age = &#39;30&#39;;
	}
	public function __clone(){
		$this->name = &#39;xfdipzone&#39;;
		$this->age = &#39;29&#39;;
	}
}
class myclass{
	public $p1;
	public $p2;
	public function __construct(){
	}
	public function __clone(){
		$this->p1 = clone $this->p1;
	}
}
$obj = new myclass();
$obj->p1 = new subclass();
$obj->p2 = new subclass();
$obj2 = clone $obj;
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
echo &#39;obj<br>&#39;;
var_dump($obj);
echo &#39;<br>obj2<br>&#39;;
var_dump($obj2);
echo &#39;ok&#39;;
echo &#39;
'; ?>
Copy after login

The above example will output:

obj
object(myclass)#1 (2) {
  ["p1"]=>
  object(subclass)#2 (2) {
    ["name":"subclass":private]=>
    string(8) "fdipzone"
    ["age":"subclass":private]=>
    string(2) "30"
  }
  ["p2"]=>
  object(subclass)#3 (2) {
    ["name":"subclass":private]=>
    string(8) "fdipzone"
    ["age":"subclass":private]=>
    string(2) "30"
  }
}
obj2
object(myclass)#4 (2) {
  ["p1"]=>
  object(subclass)#5 (2) {
    ["name":"subclass":private]=>
    string(9) "xfdipzone"
    ["age":"subclass":private]=>
    string(2) "29"
  }
  ["p2"]=>
  object(subclass)#3 (2) {
    ["name":"subclass":private]=>
    string(8) "fdipzone"
    ["age":"subclass":private]=>
    string(2) "30"
  }
}
Copy after login

As you can see, $obj2 clone $obj, $obj->p1 clone $obj->p1, and the __clone() method is executed. In the __clone method, the name and age attributes of p1 are modified, so the name and age of p1 change. Since p2 has not executed the clone() method, the properties of the newly copied $obj2->p2 are the same as $obj->p2.

Example 2: Clone the object, but some attribute references remain unchanged.

<?php
class myclass{
	public $num = null;
	public $msg = null;
	public function __construct(){
		$this->num = & $this->num;
		$this->msg = &#39;OK&#39;;
	}
	public function __clone(){
		$this->num = 2;	
	}
}
$obj = new myclass();
$obj->num = 1;
echo &#39;print obj values<br>&#39;;
var_dump($obj);
echo &#39;<br><br>&#39;;
$obj2 = clone $obj;
echo &#39;clone obj to obj2<br>&#39;;
echo &#39;obj->num:&#39;.$obj->num.&#39;<br>&#39;;
echo &#39;obj->msg:&#39;.$obj->msg.&#39;<br>&#39;;
echo &#39;obj2->num:&#39;.$obj2->num.&#39;<br>&#39;;
echo &#39;obj2->msg:&#39;.$obj2->msg.&#39;<br><br>&#39;;
$obj2->num = 3;
$obj2->msg = &#39;Yes&#39;;
echo &#39;set obj2->num=3, obj2->msg=Yes<br>&#39;;
echo &#39;obj->num:&#39;.$obj->num.&#39;<br>&#39;;
echo &#39;obj->msg:&#39;.$obj->msg.&#39;<br>&#39;;
echo &#39;obj2->num:&#39;.$obj2->num.&#39;<br>&#39;;
echo &#39;obj2->msg:&#39;.$obj2->msg.&#39;<br><br>&#39;;
?>
Copy after login

The above example will output:

print obj values
object(myclass)#1 (2) { ["num"]=> &int(1) ["msg"]=> string(2) "OK" }
clone obj to obj2
obj->num:2
obj->msg:OK
obj2->num:2
obj2->msg:OK
set obj2->num=3, obj2->msg=Yes
obj->num:3
obj->msg:OK
obj2->num:3
obj2->msg:Yes
Copy after login

Because $this->num = &$this-> ;num, so after clone(), $this->num of the new object refers to the memory address of the old object. Therefore, if the properties of the old object change, the properties of the new object will also change, so that the references to certain properties remain unchanged.
And $this->msg is not an address reference, so if the msg of the new object changes, it will not affect the old object.

Note: $this->num = & $this->num When using object attribute address reference, you cannot echo/print this attribute before cloning , otherwise the address reference will be invalid.

In the above example, if $obj2 = clone $obj is added before, echo $obj->num; will make the address reference The actual effect is that if $obj2->num changes, $obj->num will not change.

This article introduces the related content of PHP object cloning clone. For more related content, please pay attention to PHP Chinese website.

Related recommendations:

Related operations on mysql general log

Introduction to the php Cookies operation class

Introduction to PHP password generation class

The above is the detailed content of Learn about PHP object cloning clone. For more information, please follow other related articles on the PHP Chinese website!

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