Home  >  Article  >  Backend Development  >  [php classes and objects] object copy

[php classes and objects] object copy

不言
不言Original
2018-04-16 10:29:401483browse

The content of this article is about [php classes and objects] object replication, which has a certain reference value. Now I share it with everyone. Friends in need can refer to it

Object replication

Usage scenarios:

1. If you have a GTK window object, the object 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).

2. 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 object A a copy of .

Implementation: Through the clone keyword (this will call the object's __clone() method if possible), the __clone() method in the 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.

    void __clone ( 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 复制一个对象<?phpclass 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);/*
Original Object:
MyCloneable Object
(
    [object1] => SubObject Object
        (
            [instance] => 1
        )

    [object2] => SubObject Object
        (
            [instance] => 2
        )

)
*/print("Cloned Object:\n");
print_r($obj2);/*
Cloned Object:
MyCloneable Object
(
    [object1] => SubObject Object
        (
            [instance] => 3
        )

    [object2] => SubObject Object
        (
            [instance] => 2
        )

)
*/?>

Related recommendations:

[php classes and objects] magic method

[php classes and objects] traversal

[php classes and objects] overloading

The above is the detailed content of [php classes and objects] object copy. 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