php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #75145 Expand support for cloning mixed content (arrays, scalars etc.).
Submitted: 2017-09-01 12:48 UTC Modified: 2017-09-01 12:55 UTC
Votes:4
Avg. Score:3.8 ± 1.6
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: jocrutrisi at ibsats dot com Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 7.2.0RC1 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jocrutrisi at ibsats dot com
New email:
PHP Version: OS:

 

 [2017-09-01 12:48 UTC] jocrutrisi at ibsats dot com
Description:
------------
Cloning is a built-in operation in PHP, with its own token and opcode. Currently it works only on objects, but there's no reason for this restriction.

There are also some valid use cases for cloning non-object values, which are not currently addressed, namely obtaining a "clone" of an array which contains objects (more on the proposal below).

I propose:

1. Cloning continues to work on objects as it does now (no BC breaks).
2. Cloning is a NO-OP for: null, scalars; it just returns the value passed to clone. Semantically this works as null+scalars are either immutable, or copy-on-write.
3. Cloning for resources fails as it does now, because resources are not cloneable.

--- Here comes the interesting part ---

4. Cloning for arrays returns a copy of the array, *where all values that are objects have been cloned*.

Currently there's no way to obtain a comprehensive "clone" of an array containing objects like this, aside from implementing a recursive function/method to scan the array and do it (slow). 

So I think it'll be a useful addition and make clone less prone to throwing fatal errors when it semantically doesn't have to.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-01 12:52 UTC] jocrutrisi at ibsats dot com
I just wanted to add, in case it wasn't clear, object cloning when doing "clone $array" should be deep, i.e. arrays in arrays in arrays which contain objects should also be cloned.
 [2017-09-01 12:55 UTC] requinix@php.net
-Status: Open +Status: Suspended -Package: *General Issues +Package: Scripting Engine problem
 [2017-09-01 12:55 UTC] requinix@php.net
Thank you for your interest in PHP and for submitting a feature
request. Please be aware that due to the magnitude of change this
request requires, it would be necessary to discuss it on PHP
Internals list (internals@lists.php.net) as an RFC. Please read
the guide about creating RFCs here:
<https://wiki.php.net/rfc/howto>. If you haven't had experience
with writing RFCs before, it is advised to seek guidance on the
Internals list (<http://php.net/mailing-lists.php>) and/or solicit
help from one of the experienced developers. 

Please do not consider this comment as a negative view on the
merits of your proposal – every proposal which requires changes of
certain magnitude, even the very successful and widely supported
ones, must be done through the RFC process. This helps make the
process predictable, transparent and accessible to all developers.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 19:01:31 2024 UTC