
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Object Cloning
Introduction
Creating copy of an object by simple assignment merely creates another reference to the one in memory. Hence, changes in attribute reflect both in original and duplicate object. PHP has clone keyword that creates a shallow copy of the object. However, if original object has other embedded object as one of its properties, the copied object still refers to the same. To create a eep copy of object, the magic method __clone() needs to be defined in the class/
Copy by assignment
In following code, myclass has one of attributes as object of address class. An object of myclass is duplicated by assignment. Change in value of its prroperty is reflected in both objects
Example
<?php class address{ var $city="Nanded"; var $pin="431601"; function setaddr($arg1, $arg2){ $this->city=$arg1; $this->pin=$arg2; } } class myclass{ var $name="Raja"; var $obj; function setname($arg){ $this->name=$arg; } } $obj1=new myclass(); $obj1->obj=new address(); echo "original object
"; print_r($obj1); $obj2=$obj1; $obj1->setname("Ravi"); echo "after change:
"; print_r($obj1); print_r($obj2); ?>
Output
This code shows following output
original object myclass Object( [name] => Raja [obj] => address Object( [city] => Nanded [pin] => 431601 ) ) after change: original object myclass Object( [name] => Ravi [obj] => address Object( [city] => Nanded [pin] => 431601 ) ) copied object myclass Object( [name] => Ravi [obj] => address Object( [city] => Nanded [pin] => 431601 ) )
Using clone keyword
The clone keyword creates a shallow copy. Change in value of property doesn't reflect in cloned object. However, if embedded object is modified, changes are reflected in original and cloned object
Example
<?php class address{ var $city="Nanded"; var $pin="431601"; function setaddr($arg1, $arg2){ $this->city=$arg1; $this->pin=$arg2; } } class myclass{ var $name="Raja"; var $obj; function setname($arg){ $this->name=$arg; } } $obj1=new myclass(); $obj1->obj=new address(); echo "original object:
"; print_r($obj1); $obj2=clone $obj1; $obj1->setname("Ravi"); $obj1->obj->setaddr("Mumbai", "400001"); echo "after change:
"; echo "original object:
"; print_r($obj1); echo "cloned object:
"; print_r($obj2); ?>
Output
Output shows following result
original object: myclass Object( [name] => Raja [obj] => address Object( [city] => Nanded [pin] => 431601 ) ) after change: original object: myclass Object( [name] => Ravi [obj] => address Object( [city] => Mumbai [pin] => 400001 ) ) cloned object: myclass Object( [name] => Raja [obj] => address Object( [city] => Mumbai [pin] => 400001 ) )
Using __clone() method
The __clone() method creates a deep copy by creating lone of embedded object too
Example
<?php class address{ var $city="Nanded"; var $pin="431601"; function setaddr($arg1, $arg2){ $this->city=$arg1; $this->pin=$arg2; } } class myclass{ var $name="Raja"; var $obj; function setname($arg){ $this->name=$arg; } public function __clone() { $this->obj = clone $this->obj ; } } $obj1=new myclass(); $obj1->obj=new address(); echo "original object:
"; print_r($obj1); $obj2=clone $obj1; $obj1->setname("Ravi"); $obj1->obj->setaddr("Mumbai", "400001"); echo "after change:
"; echo "original object:
"; print_r($obj1); echo "cloned object:
"; print_r($obj2); ?>
Output
Output shows following result
original object: myclass Object( [name] => Raja [obj] => address Object( [city] => Nanded [pin] => 431601 ) ) after change: original object: myclass Object( [name] => Ravi [obj] => address Object( [city] => Mumbai [pin] => 400001 ) ) cloned object: myclass Object( [name] => Raja [obj] => address Object( [city] => Nanded [pin] => 431601 ) )
- Related Articles
- What is object cloning in Java?
- Advantages of Object cloning in Java
- What is the use of Object Cloning in Java?
- Explain Deep cloning an object in JavaScript with an example.
- Cloning in C#
- Cloning in Java\n
- PHP Object Inheritance
- PHP Object Interfaces
- PHP Object Iteration
- PHP Object Serialization
- PHP: Remove object from array
- Create nested JSON object in PHP?
- PHP print keys from an object?
- PHP Basics of Class and Object
- In which state was the cloning of Dolly done?
