php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #47587 When exactly is __clone() executed?
Submitted: 2009-03-06 13:25 UTC Modified: 2009-03-06 15:47 UTC
From: scout at hyperspace-travel dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2009-03-06 13:25 UTC] scout at hyperspace-travel dot de
Description:
------------
The documentation does not mention when exactly the __clone() method is performed. Neither does any of the comments. No I figure it is done AFTER shallow-copying the attributes of the old object to the new on, as everything else would be braindead. But still, it would be nice to have this information written down because that is a pretty crucial detail and it helps to understand the process of cloning in the first place.

Greets, Lene

Reproduce code:
---------------
---
From manual page: language.oop5.cloning
---



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-06 13:34 UTC] rquadling@php.net
http://docs.php.net/manual/en/language.oop5.cloning.php gives a clear 
explanation of what happens when cloning is applied to an object.

"When an object is cloned, PHP 5 will perform a shallow copy of all 
of the object's properties. Any properties that are references to 
other variables, will remain references. If a __clone() method is 
defined, then the newly created object's __clone() method will be 
called, to allow any necessary properties that need to be changed."
 [2009-03-06 13:44 UTC] scout at hyperspace-travel dot de
"If a __clone() method is 
defined, then the newly created object's __clone() method will be 
called, to allow any necessary properties that need to be changed."

Yes, I read that. But that doesn't say WHEN __clone() is executed.
 [2009-03-06 14:59 UTC] rquadling@php.net
Is this easier to read?


"When an object is cloned, PHP 5 will perform a shallow copy of all 
of the object's properties. Any properties that are references to 
other variables, will remain references.


If a __clone() method is defined, then the newly created object's 
__clone() method will be called, to allow any necessary properties 
that need to be changed."


I read this as ...

Clone runs on the copied object, which has had a shallow copy run 
against the original object.


 [2009-03-06 15:16 UTC] scout at hyperspace-travel dot de
I'd propose the following little alteration.

"When an object is cloned, PHP 5 will perform a shallow copy of all 
of the object's properties. Any properties that are references to 
other variables, will remain references.

If a __clone() method is defined, the newly created object's 
__clone() method will be called after the shallow copy, to allow any necessary properties that need to be changed."

I understood the "then" in "If... then" as a causal correlation, not as indicating a sequence of operations. It IS an ambiguous formulation, after all.
 [2009-03-06 15:24 UTC] rquadling@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

Text changed to:

When an object is cloned, PHP 5 will perform a shallow copy of all of 
the object's properties. Any properties that are references to other 
variables, will remain references.

Once the cloning is complete, if a __clone() method is defined, then 
the newly created object's __clone() method will be called, to allow 
any necessary properties that need to be changed.


 [2009-03-06 15:47 UTC] scout at hyperspace-travel dot de
Thank you!
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 00:00:02 2026 UTC