{"id":97,"date":"2021-03-08T00:18:26","date_gmt":"2021-03-08T00:18:26","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=97"},"modified":"2021-05-21T06:47:22","modified_gmt":"2021-05-21T06:47:22","slug":"php-clone-object","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/php-clone-object\/","title":{"rendered":"PHP Clone Object"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to clone an object using the clone keyword in PHP.<\/p>\n\n\n\n<p>To clone an <a href=\"https:\/\/phptutorial.net\/php-oop\/php-objects\/\">object<\/a> is to create a copy of an object. The <code>clone<\/code> keyword allows you to perform a <strong>shallow copy<\/strong> of an object. By combining the <code>clone<\/code> keyword and <code>__clone()<\/code> magic method, you can perform a deep copy of an object.<\/p>\n\n\n\n<p>It&#8217;ll be easier to understand the clone, shallow copy, and deep copy concepts via examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='setting-up'>Setting up <a href=\"#setting-up\" class=\"anchor\" id=\"setting-up\" title=\"Anchor for Setting up\">#<\/a><\/h2>\n\n\n\n<p>The following example defines a simple <code>Person<\/code> class that has one property <code>$name<\/code>. To make it simple, we&#8217;ll make the <code>$name<\/code> property public:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> $name;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($name)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;name = $name;\n\t}\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='copying-object-via-assignment'>Copying object via assignment <a href=\"#copying-object-via-assignment\" class=\"anchor\" id=\"copying-object-via-assignment\" title=\"Anchor for Copying object via assignment\">#<\/a><\/h2>\n\n\n\n<p>The following illustrates how to copy an object via the <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-assignment-operators\/\">assignment opeator<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> $name;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($name)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;name = $name;\n\t}\n}\n\n$bob = <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Bob'<\/span>);\n<span class=\"hljs-comment\">\/\/ assign bob to alex and change the name<\/span>\n$alex = $bob;\n\n\n$alex-&gt;name = <span class=\"hljs-string\">'Alex'<\/span>;\n\n<span class=\"hljs-comment\">\/\/ show both objects<\/span>\nvar_dump($bob);\nvar_dump($alex);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">object(Person)<span class=\"hljs-comment\">#1 (1) {<\/span>\n  &#91;<span class=\"hljs-string\">\"name\"<\/span>]=&gt; string(<span class=\"hljs-number\">4<\/span>) <span class=\"hljs-string\">\"Alex\"<\/span>\n}\nobject(Person)<span class=\"hljs-comment\">#1 (1) {<\/span>\n  &#91;<span class=\"hljs-string\">\"name\"<\/span>]=&gt; string(<span class=\"hljs-number\">4<\/span>) <span class=\"hljs-string\">\"Alex\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The var_dump() shows one object with the #1.<\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, create a new instance of the <code>Person<\/code> class called <code>$bob<\/code> with the <code>$name<\/code> property sets to <code>'Bob'<\/code>.<\/li><li>Second, assign <code>$bob<\/code> to <code>$alex<\/code> and change the value of the <code>$name<\/code> property to <code>'Alex'<\/code>.<\/li><li>Third, use the <code>var_dump()<\/code> function to show both objects.<\/li><\/ul>\n\n\n\n<p>In this example, both <code>$bob<\/code> and <code>$alex<\/code> reference the same object in the memory. When we change the property of an object, it reflects in both references.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"242\" height=\"121\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/03\/PHP-clone-Object-assignment.png\" alt=\"\" class=\"wp-image-552\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='copying-object-using-the-clone-keyword'>Copying object using the clone keyword <a href=\"#copying-object-using-the-clone-keyword\" class=\"anchor\" id=\"copying-object-using-the-clone-keyword\" title=\"Anchor for Copying object using the clone keyword\">#<\/a><\/h2>\n\n\n\n<p>PHP provides you with the <code>clone<\/code> keyword that allows you to create a shallow copy of an object. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$bob = <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Bob'<\/span>);\n\n<span class=\"hljs-comment\">\/\/ clone an object<\/span>\n$alex = <span class=\"hljs-keyword\">clone<\/span> $bob;\n$alex-&gt;name = <span class=\"hljs-string\">'Alex'<\/span>;\n\n<span class=\"hljs-comment\">\/\/ show both objects<\/span>\nvar_dump($bob);\nvar_dump($alex);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">object(Person)<span class=\"hljs-comment\">#1 (1) {<\/span>\n  &#91;<span class=\"hljs-string\">\"name\"<\/span>]=&gt; string(<span class=\"hljs-number\">3<\/span>) <span class=\"hljs-string\">\"Bob\"<\/span>\n}\nobject(Person)<span class=\"hljs-comment\">#2 (1) {<\/span>\n  &#91;<span class=\"hljs-string\">\"name\"<\/span>]=&gt; string(<span class=\"hljs-number\">4<\/span>) <span class=\"hljs-string\">\"Alex\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>clone<\/code> keyword creates a copy of the <code>Person<\/code> object. There are two objects in the memory. Therefore, changing the property of one object doesn&#8217;t affect the other:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"242\" height=\"122\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/03\/PHP-clone-Object-clone.png\" alt=\"\" class=\"wp-image-553\"\/><\/figure>\n\n\n\n<p>The <code>var_dump()<\/code> also shows the object #1 and #2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='php-__clone-magic-method'>PHP __clone magic method <a href=\"#php-__clone-magic-method\" class=\"anchor\" id=\"php-__clone-magic-method\" title=\"Anchor for PHP __clone magic method\">#<\/a><\/h3>\n\n\n\n<p>The <code>__clone()<\/code> is a magic method with the following syntax: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__clone<\/span> (<span class=\"hljs-params\"> <\/span>) : <span class=\"hljs-title\">void<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you define the <code>clone()<\/code> method, PHP will execute it automatically when the cloning completes. The <code>clone()<\/code> is useful when you want to change the properties of the copied object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='shallow-copy'>Shallow copy <a href=\"#shallow-copy\" class=\"anchor\" id=\"shallow-copy\" title=\"Anchor for Shallow copy\">#<\/a><\/h2>\n\n\n\n<p>As mentioned ealier, the <code>clone<\/code> performs a shallow copy of an object. It means that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a copy of all properties of an object.<\/li><li>If a property references another object, the property remains a reference.<\/li><\/ul>\n\n\n\n<p>In other words, when an object has a property that references another object, that property remains a reference after cloning.<\/p>\n\n\n\n<p>Let&#8217;s see the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Address<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> $street;\n\n\t<span class=\"hljs-keyword\">public<\/span> $city;\n}\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> $name;\n\n\t<span class=\"hljs-keyword\">public<\/span> $address;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($name)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;name = $name;\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;address = <span class=\"hljs-keyword\">new<\/span> Address();\n\t}\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, define a new class called <code>Address<\/code> that has two properties <code>$city<\/code> and <code>$street<\/code>.<\/li><li>Second, change the <code>Person<\/code> class by adding the <code>address<\/code> property. In the constructor, initialize the <code>address<\/code> to a new <code>Address<\/code> object.<\/li><\/ul>\n\n\n\n<p>The <code>Person<\/code> class has the <code>address<\/code> property as a reference.<\/p>\n\n\n\n<p>The following creates a new <code>Person<\/code> object called <code>$bob<\/code> and assigns the properties of the <code>address<\/code> property:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$bob = <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Bob'<\/span>);\n$bob-&gt;address-&gt;street = <span class=\"hljs-string\">'North 1st Street'<\/span>;\n$bob-&gt;address-&gt;city = <span class=\"hljs-string\">'San Jose'<\/span>;\n\nvar_dump($bob);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">object(Person)<span class=\"hljs-comment\">#1 (2) {<\/span>\n  &#91;<span class=\"hljs-string\">\"name\"<\/span>]=&gt;  string(<span class=\"hljs-number\">3<\/span>) <span class=\"hljs-string\">\"Bob\"<\/span>\n  &#91;<span class=\"hljs-string\">\"address\"<\/span>]=&gt; object(Address)<span class=\"hljs-comment\">#2 (2) {        <\/span>\n    &#91;<span class=\"hljs-string\">\"street\"<\/span>]=&gt; string(<span class=\"hljs-number\">16<\/span>) <span class=\"hljs-string\">\"North 1st Street\"<\/span>\n    &#91;<span class=\"hljs-string\">\"city\"<\/span>]=&gt; string(<span class=\"hljs-number\">8<\/span>) <span class=\"hljs-string\">\"San Jose\"<\/span>\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>var_dump()<\/code> shows two objects <code>Person<\/code> (#1) and <code>Address<\/code>(#2). The <code>Person<\/code> object has the <code>address<\/code> property that references the <code>Address<\/code> object.<\/p>\n\n\n\n<p>The following creates a copy of the <code>$bob<\/code> object and assigns it to <code>$alex<\/code>. It also changes the value of the <code>$name<\/code> property to <code>'Alex'<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$alex = <span class=\"hljs-keyword\">clone<\/span> $bob;\n$alex-&gt;name = <span class=\"hljs-string\">'Alex'<\/span>;\n\nvar_dump($alex);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">object(Person)<span class=\"hljs-comment\">#3 (2) {<\/span>\n  &#91;<span class=\"hljs-string\">\"name\"<\/span>]=&gt; string(<span class=\"hljs-number\">4<\/span>) <span class=\"hljs-string\">\"Alex\"<\/span>\n  &#91;<span class=\"hljs-string\">\"address\"<\/span>]=&gt; object(Address)<span class=\"hljs-comment\">#2 (2) {<\/span>\n    &#91;<span class=\"hljs-string\">\"street\"<\/span>]=&gt; string(<span class=\"hljs-number\">16<\/span>) <span class=\"hljs-string\">\"North 1st Street\"<\/span>\n    &#91;<span class=\"hljs-string\">\"city\"<\/span>]=&gt; string(<span class=\"hljs-number\">8<\/span>) <span class=\"hljs-string\">\"San Jose\"<\/span>\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>var_dump()<\/code> shows the new <code>Person<\/code> object (#3) which is a copy of the Person object (#1). However, the <code>address<\/code> property of the new <code>Person<\/code> object still references the same <code>Address<\/code> object:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"432\" height=\"132\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/03\/PHP-clone-Object-shallow-copy-1.png\" alt=\"\" class=\"wp-image-570\" srcset=\"https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/03\/PHP-clone-Object-shallow-copy-1.png 432w, https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/03\/PHP-clone-Object-shallow-copy-1-300x92.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><\/figure>\n\n\n\n<p>It means that both <code>Person<\/code> objects have the <code>address<\/code> property that references the same <code>Address<\/code> object. Changing the <code>Address<\/code> object from <code>$alex<\/code> will affect <code>$bob<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$alex-&gt;address-&gt;street = <span class=\"hljs-string\">'1 Apple Park Way'<\/span>;\n$alex-&gt;address-&gt;city = <span class=\"hljs-string\">'Cupertino'<\/span>;\n\nvar_dump($bob);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">object(Person)<span class=\"hljs-comment\">#1 (2) {<\/span>\n  &#91;<span class=\"hljs-string\">\"name\"<\/span>]=&gt; string(<span class=\"hljs-number\">3<\/span>) <span class=\"hljs-string\">\"Bob\"<\/span> \n  &#91;<span class=\"hljs-string\">\"address\"<\/span>]=&gt; object(Address)<span class=\"hljs-comment\">#2 (2) {<\/span>\n    &#91;<span class=\"hljs-string\">\"street\"<\/span>]=&gt; string(<span class=\"hljs-number\">16<\/span>) <span class=\"hljs-string\">\"1 Apple Park Way\"<\/span>\n    &#91;<span class=\"hljs-string\">\"city\"<\/span>]=&gt; string(<span class=\"hljs-number\">9<\/span>) <span class=\"hljs-string\">\"Cupertino\"<\/span>\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Put it all together:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Address<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> $street;\n\n\t<span class=\"hljs-keyword\">public<\/span> $city;\n}\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> $name;\n\n\t<span class=\"hljs-keyword\">public<\/span> $address;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($name)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;name = $name;\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;address = <span class=\"hljs-keyword\">new<\/span> Address();\n\t}\n}\n\n$bob = <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Bob'<\/span>);\n$bob-&gt;address-&gt;street = <span class=\"hljs-string\">'North 1st Street'<\/span>;\n$bob-&gt;address-&gt;city = <span class=\"hljs-string\">'San Jose'<\/span>;\n\nvar_dump($bob);\n\n$alex = <span class=\"hljs-keyword\">clone<\/span> $bob;\n$alex-&gt;name = <span class=\"hljs-string\">'Alex'<\/span>;\n\nvar_dump($alex);\n\n$alex-&gt;address-&gt;street = <span class=\"hljs-string\">'1 Apple Park Way'<\/span>;\n$alex-&gt;address-&gt;city = <span class=\"hljs-string\">'Cupertino'<\/span>;\n\nvar_dump($bob);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='deep-copy-with-__clone-method'>Deep copy with __clone method <a href=\"#deep-copy-with-__clone-method\" class=\"anchor\" id=\"deep-copy-with-__clone-method\" title=\"Anchor for Deep copy with __clone method\">#<\/a><\/h2>\n\n\n\n<p>Deep copy creates a copy of an object and recursively creates a copy of the objects referenced by the properties of the object.<\/p>\n\n\n\n<p>Since PHP calls the <code>__clone()<\/code> method automatically after cloning an object, you can clone the objects referenced by the properties of the class.<\/p>\n\n\n\n<p>The following example illustrates how to use the <code>__clone()<\/code> magic method to carry a deep copy of the <code>Person<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Address<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> $street;\n\n\t<span class=\"hljs-keyword\">public<\/span> $city;\n}\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> $name;\n\n\t<span class=\"hljs-keyword\">public<\/span> $address;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($name)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;name = $name;\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;address = <span class=\"hljs-keyword\">new<\/span> Address();\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__clone<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;address = <span class=\"hljs-keyword\">clone<\/span> <span class=\"hljs-keyword\">$this<\/span>-&gt;address;\n\t}\n}\n\n$bob = <span class=\"hljs-keyword\">new<\/span> Person(<span class=\"hljs-string\">'Bob'<\/span>);\n$bob-&gt;address-&gt;street = <span class=\"hljs-string\">'North 1st Street'<\/span>;\n$bob-&gt;address-&gt;city = <span class=\"hljs-string\">'San Jose'<\/span>;\n\n$alex = <span class=\"hljs-keyword\">clone<\/span> $bob;\n$alex-&gt;name = <span class=\"hljs-string\">'Alex'<\/span>;\n$alex-&gt;address-&gt;street = <span class=\"hljs-string\">'1 Apple Park Way'<\/span>;\n$alex-&gt;address-&gt;city = <span class=\"hljs-string\">'Cupertino'<\/span>;\n\nvar_dump($bob);\nvar_dump($alex);\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">object(Person)<span class=\"hljs-comment\">#1 (2) {<\/span>\n  &#91;<span class=\"hljs-string\">\"name\"<\/span>]=&gt; string(<span class=\"hljs-number\">3<\/span>) <span class=\"hljs-string\">\"Bob\"<\/span>\n  &#91;<span class=\"hljs-string\">\"address\"<\/span>]=&gt; object(Address)<span class=\"hljs-comment\">#2 (2) {<\/span>\n    &#91;<span class=\"hljs-string\">\"street\"<\/span>]=&gt; string(<span class=\"hljs-number\">16<\/span>) <span class=\"hljs-string\">\"North 1st Street\"<\/span>\n    &#91;<span class=\"hljs-string\">\"city\"<\/span>]=&gt; string(<span class=\"hljs-number\">8<\/span>) <span class=\"hljs-string\">\"San Jose\"<\/span>\n  }\n}\n\nobject(Person)<span class=\"hljs-comment\">#3 (2) {<\/span>\n  &#91;<span class=\"hljs-string\">\"name\"<\/span>]=&gt; string(<span class=\"hljs-number\">4<\/span>) <span class=\"hljs-string\">\"Alex\"<\/span>\n  &#91;<span class=\"hljs-string\">\"address\"<\/span>]=&gt; object(Address)<span class=\"hljs-comment\">#4 (2) {<\/span>\n    &#91;<span class=\"hljs-string\">\"street\"<\/span>]=&gt; string(<span class=\"hljs-number\">16<\/span>) <span class=\"hljs-string\">\"1 Apple Park Way\"<\/span>\n    &#91;<span class=\"hljs-string\">\"city\"<\/span>]=&gt; string(<span class=\"hljs-number\">9<\/span>) <span class=\"hljs-string\">\"Cupertino\"<\/span>\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The new here is <code>__clone()<\/code> method in the Person class. The <code>__clone()<\/code> method create a copy of the <code>Address<\/code> object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='deep-copy-using-serialize-and-unserialize-functions'>Deep copy using serialize and unserialize functions <a href=\"#deep-copy-using-serialize-and-unserialize-functions\" class=\"anchor\" id=\"deep-copy-using-serialize-and-unserialize-functions\" title=\"Anchor for Deep copy using serialize and unserialize functions\">#<\/a><\/h2>\n\n\n\n<p>Another way to carry a deep copy of an object is to use the <code>serialize()<\/code> and <code>unserialize()<\/code> functions. <\/p>\n\n\n\n<p>The <code>serialize()<\/code> function creates a storable representation of an object while the unserialize() function creates an object from the storable value.<\/p>\n\n\n\n<p>The following <code>deep_clone()<\/code> function creates a deep copy of an object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">deep_clone<\/span><span class=\"hljs-params\">($object)<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">return<\/span> unserialize(serialize($object));\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use <code>clone<\/code>  to perform a shallow copy of an object.<\/li><li>Combine <code>clone<\/code> and <code>__clone()<\/code> method to create a deep copy of an object.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"97\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-clone-object\/\"\n\t\t\t\tdata-post-title=\"PHP Clone Object\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"97\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-clone-object\/\"\n\t\t\t\tdata-post-title=\"PHP Clone Object\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use PHP clone object to create a copy of an object.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1753,"menu_order":25,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-97","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":11,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/97\/revisions"}],"predecessor-version":[{"id":572,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/97\/revisions\/572"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}