·  PHP – Syntax – OOP – Terms – Object Cloning

 

- Info:

  - From PHP 5 all Objects are treated by default as references which allows use of design patterns.

  - To copy object you have to clone it by prefacing it with the clone keyword, like so:

 

- Single File:

  - This example shoes efects of referencing and cloning.

  - Creat PHP Console Application Test.php.

 

    Person.php

    <?php

   

      //CLASS.

      class Person {

        public $name = "John";

      }

      

      //OBJECT. 

      $john = new Person();

       

      //COPY BY CLONING.

      $jack = clone $john;

      $jack->name = "Jill"//This will not change $john->name

      echo($john->name);     //John

      

      //REFERENCE 

      $jill = $john;         //Jill is only reference to john.

      $jill->name = "Jill"//This also changes $john->name

      echo($john->name);     //Jill

      

    ?>