How to create a copy of an object in PHP? Last Updated : 01 Dec, 2021 Comments Improve Suggest changes Like Article Like Report An object copy is created by using the clone keyword (which calls the object's __clone() method if possible). An object's __clone() method cannot be called directly. When an object is cloned, PHP will perform a shallow copy of all of the object's properties. Any properties that are references to other variables will remain references.Syntax: $copy_object_name = clone $object_to_be_copied Program 1: Program to create copy of an object. php <?php // Program to create copy of an object // Creating class class GFG { public $data1; public $data2; public $data3; } // Creating object $obj = new GFG(); // Creating clone or copy of object $copy = clone $obj; // Set values of $obj object $obj->data1 = "Geeks"; $obj->data2 = "for"; $obj->data3 = "Geeks"; // Set values of copied object $copy->data1 = "Computer "; $copy->data2 = "science "; $copy->data3 = "portal"; // Print values of $obj object echo "$obj->data1$obj->data2$obj->data3\n"; // Print values of $copy object echo "$copy->data1$copy->data2$copy->data3\n"; ?> Output: GeeksforGeeks Computer science portal Example 2: Below program distinguishes clone from assignment ( = ) operator. php <?php // Program to create copy of an object // Creating class class GFG { public $data1; public $data2; public $data3; } // Creating object $obj = new GFG(); // Creating clone or copy of object $copy = clone $obj; // Creating object without clone keyword $obj_ref = $obj; // Set values of $obj object $obj->data1 = "Geeks"; $obj->data2 = "for"; $obj->data3 = "Geeks"; // Set values of copied object $copy->data1 = "Python "; $copy->data2 = "for "; $copy->data3 = "Machine learning"; // Print values of $obj object echo "$obj->data1$obj->data2$obj->data3\n"; // Print values of $copy object echo "$copy->data1$copy->data2$copy->data3\n"; // Print values of without clone object echo "$obj_ref->data1$obj_ref->data2$obj_ref->data3\n"; ?> Output: GeeksforGeeks Python for Machine learning GeeksforGeeks Note: It is clear that cloned object have different values than original object but original and referenced object created by using '=' operator have same value. References:https://www.php.net/manual/en/language.oop5.cloning.php Comment More infoAdvertise with us Next Article How to create a copy of an object in PHP? R Rajnis09 Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to copy a DateTime object in PHP ? Given a DateTime object and the task is to create a copy of that object. To create a copy of DateTime object we use clone keyword which is described below:A copy of DateTime object is created by using the clone keyword (which calls the objectâs __clone() method if possible). An objectâs __clone() me 3 min read How to Define an Empty Object in PHP ? An object is an instance of a class, and we can create objects using various approaches. Defining an empty object typically involves creating an instance without specifying a class definition explicitly. In this article, we will explore different approaches to defining an empty object in PHP, these 1 min read How to Create an Array of Given Size in PHP? This article will show you how to create an array of given size in PHP. PHP arrays are versatile data structures that can hold multiple values. Sometimes, it's necessary to create an array of a specific size, either filled with default values or left empty. Table of Content Using array_fill() Functi 3 min read Convert an object to associative array in PHP An object is an instance of a class. It is simply a specimen of a class and has memory allocated. The array is the data structure that stores one or more similar types of values in a single name but an associative array is different from a simple PHP array. An array that contains a string index is c 3 min read How to Pass an Array into a Function in PHP ? This article will show you how to pass an array to function in PHP. When working with arrays, it is essential to understand how to pass them into functions for effective code organization and re-usability. This article explores various approaches to pass arrays into PHP functions, covering different 2 min read Like