Home  >  Article  >  Backend Development  >  What does the clone method in php do?

What does the clone method in php do?

藏色散人
藏色散人Original
2020-09-02 10:38:333118browse

The role of the clone method in php: used for object copying. Object copying is completed through the clone keyword, such as [$copy_of_object = clone $object;]. The __clone() method in an object cannot be called directly.

What does the clone method in php do?

php clone method is used for object copying

Recommended: "php video tutorial"

In most cases, we don't need to completely copy an object to obtain its properties. But there is one case where it is really needed: if you have a GTK window object that holds window-related resources. You may want to copy a new window, keeping all the same properties as the original window, but it must be a new object (because if it is not a new object, changes in one window will affect the other window).

There is another situation: if object A stores a reference to object B, when you copy object A, and you want the object used in it to be no longer object B but a copy of B, then you must Get a copy of object A.

Object copying can be done via the clone keyword (this will call the object's __clone() method if possible). The __clone() method in an object cannot be called directly.

$copy_of_object = clone $object;

When an object is copied, PHP 5 will perform a shallow copy of all properties of the object. All reference properties will still be references to the original variables.

__clone ( void ) : void

When copying is completed, if the __clone() method is defined, the __clone() method in the newly created object (the object generated by copying) will be called and can be used to modify the value of the attribute ( if necessary).

Example #1 Copy an object

<?php
class SubObject
{
    static $instances = 0;
    public $instance;
    public function __construct() {
        $this->instance = ++self::$instances;
    }
    public function __clone() {
        $this->instance = ++self::$instances;
    }
}
class MyCloneable
{
    public $object1;
    public $object2;
    function __clone()
    {
      
        // 强制复制一份this->object, 否则仍然指向同一个对象
        $this->object1 = clone $this->object1;
    }
}
$obj = new MyCloneable();
$obj->object1 = new SubObject();
$obj->object2 = new SubObject();
$obj2 = clone $obj;
print("Original Object:\n");
print_r($obj);
print("Cloned Object:\n");
print_r($obj2);
?>

The above routine will output:

Original Object:
MyCloneable Object
(
    [object1] => SubObject Object
        (
            [instance] => 1
        )
    [object2] => SubObject Object
        (
            [instance] => 2
        )
)
Cloned Object:
MyCloneable Object
(
    [object1] => SubObject Object
        (
            [instance] => 3
        )
    [object2] => SubObject Object
        (
            [instance] => 2
        )
)

The above is the detailed content of What does the clone method in php do?. For more information, please follow other related articles on the PHP Chinese website!

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