PHP Objects and Cloning

My daily cup of tea

Object cloning in PHP works slightly different than other languages I’ve used so far. It is reasonable enough to make this small note to remind myself of the variant in PHP:

1. Creating a new object will allocate some memory for the object properties. The object pointer will point to these memory blocks to get/set property values

2. Clone the object by $clonedInstance = $originalInstance; This would create a new object pointer that will point to the same data structure as the original instance

3. Now assign a value to $originalInstance’s property. The same value will automatically be reflected to $clonedInstance (even though the value was assigned post object cloning) because they are pointing to the same object

4. Unset $originalInstance. The object pointer to the memory blocks that hold values of the properties will be destroyed. But – the memory still retains the values of the properties. When referring…

View original post 12 more words


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s