Req #75145 [Com]: Expand support for cloning mixed content (arrays, scalars etc.).

From: Date: Fri, 01 Sep 2017 12:52:46 +0000
Subject: Req #75145 [Com]: Expand support for cloning mixed content (arrays, scalars etc.).
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-210903@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75145&edit=1 ID: 75145 Comment by: jocrutrisi at ibsats dot com Reported by: jocrutrisi at ibsats dot com Summary: Expand support for cloning mixed content (arrays, scalars etc.). Status: Open Type: Feature/Change Request Package: *General Issues PHP Version: 7.2.0RC1 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2017-09-01 12:48:05] 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75145&edit=1

« previous php.bugs (#210903) next »