PHP Clone deep copy and shallow copy ccllone hdclone memberwiseclon

WBOY
Release: 2016-07-29 08:54:05
Original
1362 people have browsed it

Today when I was looking at the design patterns of Dahua, I saw the prototype pattern. It talked about deep copy and shallow copy, so I searched the PHP manual.
See how PHP implements deep copy and shallow copy.

<code><span><span>class</span><span>SubObject</span>
{</span><span>static</span><span>$instances</span> = <span>0</span>;
    <span>public</span><span>$instance</span>;

    <span>public</span><span><span>function</span><span>__construct</span><span>()</span>
    {</span><span>$this</span>->instance = ++ <span>self</span>::<span>$instances</span>;
    }

    <span>public</span><span><span>function</span><span>__clone</span><span>()</span>
    {</span><span>$this</span>->instance = ++<span>self</span>::<span>$instances</span>;
    }
}

<span><span>class</span><span>MyCloneable</span>
{</span><span>public</span><span>$object1</span>;
    <span>public</span><span>$object2</span>;

    <span>public</span><span><span>function</span><span>__clone</span><span>()</span>
    {</span><span>// 强制复制一份this->object, 否则仍然指向同一个对象</span><span>// 深复制[将自己的属性重新克隆一份]</span><span>$this</span>->object1 = <span>clone</span><span>$this</span>->object1;
        <span>// 浅复制[引用的还是原来,并没有生成新的]</span><span>//$this->object2 = clone $this->object2;</span>
    }

}

<span>$obj</span> = <span>new</span> MyCloneable();

<span>$obj</span>->object1 = <span>new</span> SubObject();
<span>$obj</span>->object2 = <span>new</span> SubObject();

<span>$obj2</span> = <span>clone</span><span>$obj</span>;

<span>print</span>(<span>"Original Object:\n"</span>);
print_r(<span>$obj</span>);

<span>print</span>(<span>"Clone Object:\n"</span>);
print_r(<span>$obj2</span>);</code>
Copy after login

result

<code>Original Object:
MyCloneable Object
(
    [object1] => SubObject Object
        (
            [instance] => <span>1</span>
        )

    [object2] => SubObject Object
        (
            [instance] => <span>2</span>
        )

)
<span>Clone</span> Object:
MyCloneable Object
(
    [object1] => SubObject Object
        (
            [instance] => <span>3</span><span>//深复制 所以有+1</span>
        )

    [object2] => SubObject Object
        (
            [instance] => <span>2</span><span>//浅复制 并没有创建新的对象</span>
        )

)
</code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces PHP Clone deep copy and shallow copy, including clone and php content. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!