Back to Top

How to create Object Copy in PHP

Cloning Object in PHP

Today I was creating some OOP demos for my students and I have created one demo for Object cloning concept in PHP. When we create an object with the use of the new keyword, the returned value is a handle to an object.

What is Clone in PHP?

PHP 5 supports cloning of object. In PHP, Object cloning needed to make a copy of an object is to assign it to a new variable. that means each instance of the object has an independent value and modifying one has no effect upon the other.

In PHP, All the objects are treated as the reference instead of the value so it is more difficult to copy the object. If you try to copy a reference to the object, which would point to the original object. In order to solve this problem, PHP provides a clone objects method.

You can add keywords to clone an object in PHP as follows:

SYNTAX:

How to use __clone method in PHP

Object cloning is to create a copy of an existing object.To Clone, an object is possible with the keyword or language construct clone.The built in clone keyword automatically generates a new instance of the object with its own copy of the properties.PHP also provide one magic method __clone() which can call when you clone the object with clone keyword.__clone() the method will be called during the clone process.

Let’s see how we can use __clone method

Remember that A clone object is a shallow copy of the object, not a deep copy that means it creates a reference to the same memory location as the object.we need a copy of the object rather than a copy of the reference, clone comes in picture.

What are the differences between Object Copy and Clone

We can copy the object with the = operator and will give the result as second variable reference the same object.A copy of object means to every time we assigning one variable to another variable.Like in below example,$Objc = $Obj means the first object is the copy of the second object.However, the value of both objects $Obj and $Objc are same because when changing $Objc, you actually change the same object $Obj is referencing.

Let’s check copy of object by example

Next, let’s see Object cloning by example:

Here, original $Obj object remains unaffected by second object $objc and its value remains same.

Remember: The Object clone will throw an E_ERROR error when run in PHP 4 because its not exist in PHP 4.

You would also like to read another object oriented concept which is abstract methods in PHP.

So, what about you? Have you used Object cloning in your application? Share your experience about Clone method is a comment section.

Do subscribe to our Email newsletter to receive more about PHP articles.In case, if you have questions feel free to ask through comments.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to Create a Custom WordPress Widget

Posted on 11 years ago

Bhumi

How to setup Facebook PageTab Popup

Posted on 11 years ago

Bhumi

How to add Tags in WordPress Pages?

Posted on 11 years ago

Bhumi