Jan Brinkmann
By Jan Brinkmann Last updated on Shown in: php 7

In the field of object-oriented programming, understanding references and working with objects plays a crucial role. PHP, as a popular programming language for web development, allows you to effectively utilize the concepts of object orientation. In this text, you will learn how to create and copy objects – and why cloning objects is important.

Object-oriented web programming with PHP - 6.08 Duplicating objects

The video is only loaded from YouTube when you play it – data (including your IP address) is then transmitted to YouTube/Google. More in the privacy policy

Key Insights

Duplicating objects in PHP does not happen by simply assigning variables. Instead, a reference is copied that points to the same object. To actually create a new instance of an object, you must use the keyword clone.

Step-by-Step Guide

To understand and apply cloning of objects in PHP, follow these steps:

Step 1: Creating a Class

First, you need to create a class that serves as a template for your object. This can be a simple class with one or more attributes.

Cloning objects in PHP through effective management

Step 2: Instantiating Objects

Create an object based on the defined class. The object will acquire some properties. For example, set the name of the object to "Jan".

Step 3: Copying an Object

However, only the reference of $user1 is passed to $user2. This means both variables point to the same object. If you now change the name of $user2, the name of $user1 will also change.

Step 4: Checking the Values

To verify that both variables point to the same object, you can check the properties in the output of a browser. Change the name in one of the variables and see what happens.

Cloning objects in PHP through effective management

Step 5: Cloning Objects

If you want to create an independent object based on an existing object, you use the keyword clone.

Here, $user2 is now a new instance with its own values, independent of $user1.

Step 6: Verifying Results

Reload the page to determine that $user1 remains unchanged while $user2 has the newly assigned values. This shows that the two class instances are now independent.

Summary - Object-Oriented Web Programming: Cloning Objects

Cloning objects in PHP is an essential part of object-oriented programming. It allows you to duplicate data structures without affecting the original instance. Using the clone keyword ensures that you receive a new instance of the object.

Frequently Asked Questions

How do I copy an object in PHP?You use the keyword clone to create a true copy of an object.

What happens if I simply assign an object?A reference to the original object is created, not a copy.

Can I customize the clone keyword?Yes, you can implement the __clone() method in your class to customize cloning.

How does cloning affect performance?Cloning consumes more memory and may affect performance if many objects are cloned.

Jan Brinkmann
Trainer About Jan Brinkmann Web development with passion! Jan has been a developer for over 15 years. All courses by Jan Brinkmann