Change language

How do I create a copy of an object in PHP?

Syntax:
$copy_object_name = clone $object_to_be_copied 
Program 1:Program for creating a copy of an object.  
// Program for creating a copy of the object 
/ / Create a class class GFG { public $data1 ; public $data2 ; public $data3 ; }  
// Create an object $obj = new GFG();  
// Create a clone or copy of the object $copy = clone $obj ;  
// Set the values ​​of the $obj object $obj -> data1 = "Geeks" ; $obj -> data2 = "for" ; $obj -> data3 = "Geeks" ;  
// Set the values ​​of the copied object $copy -> data1 = "Computer" ; $copy -> data2 = "science" ; $copy -> data3 = "portal" ;  
// Display the values ​​of the $obj object echo "$obj-> data1 $obj-> data2 $obj-> data3" ;  
// Print object values ​​$copy echo "$copy-> data1 $copy-> data2 $copy-> data3" ;  
?>
Exit:
GeeksforGeeks Computer science portal 
Example 2:The program below distinguishes clone from the assignment operator (=).  
// Program for creating a copy of the object  
// Create a class class GFG { public $data1 ; public $data2 ; public $data3 ;  
}  
// Create an object $obj = new GFG();  
// Create a clone or copy of the object $copy = clone $obj ;  
// Create an object without the clone keyword $obj_ref = $obj ;  
// Set the values ​​of the $obj object $obj -> data1 = "Geeks" ; $obj -> data2 = "for" ; $obj -> data3 = "Geeks" ;  
// Set the values ​​of the copied object $copy -> data1 = "Python" ; $copy -> data2 = "for" ; $copy -> data3 = "Machine learning" ;  
// Display the values ​​of the $obj object echo "$obj-> data1 $obj-> data2 $obj-> data3" ;  
// Print object values ​​$copy echo "$copy-> data1 $copy-> data2 $copy-> data3" ;  
// Print values ​​without clone echo "$obj_ref-> data1 $obj_ref-> data2 $obj_ref-> data3" ;  
?>
Exit:
GeeksforGeeks Python for Machine learning GeeksforGeeks 
Note.It is clear that the cloned object has different values ​​from the original, but the original and updated object created using the operator ’ = ’ , have the same meaning.Links: https://www.php.net/manual/en/language.oop5.cloning.php

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method