Bug #66116 [Opn->Nab]: Cloning object leaving resource reference
| From: | requinix@php.net | Date: | Tue, 19 Nov 2013 18:56:11 +0000 |
| Subject: | Bug #66116 [Opn->Nab]: Cloning object leaving resource reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-182837@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66116&edit=1
ID: 66116
Updated by: requinix@php.net
Reported by: joni2back at gmail dot com
Summary: Cloning object leaving resource reference
-Status: Open
+Status: Not a bug
Type: Bug
Package: Variables related
Operating System: Debian 6
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Clones are not deep copies. You are responsible for "cloning" resources and the like via
__clone().
http://www.php.net/language.oop5.cloning
Previous Comments:
------------------------------------------------------------------------
[2013-11-19 11:47:11] joni2back at gmail dot com
Description:
------------
When object is cloned and it have a property that handle resource
The resource is not cloned, remains the same of the previous instance
This could affect dependence resource options (for example setting curl url in an instance will set
all cloned instnaces, closing file, etc)
Test script:
---------------
<?php
class ResourceA
{
public $resource, $id;
function __construct()
{
//for example i use curl resource to change and view handler options
$this->id = md5(rand());
$this->resource = curl_init();
curl_setopt($this->resource, CURLOPT_URL, $this->id);
}
}
class ResourceB extends ResourceA
{
}
/** ******************************************** **/
$resourceA = new ResourceA;
$resourceB = new ResourceB;
$resourceC = clone $resourceA;
//Setting an option to this resource shouldn't affect ResourceA resource that was cloned
$resourceC->id = md5(rand());
curl_setopt($resourceC->resource, CURLOPT_URL, $resourceC->id);
print_r(array(
array($resourceA, 'RESOURCEPROP_'.curl_getinfo($resourceA->resource,
CURLINFO_EFFECTIVE_URL)),
array($resourceB, 'RESOURCEPROP_'.curl_getinfo($resourceB->resource,
CURLINFO_EFFECTIVE_URL)),
array($resourceC, 'RESOURCEPROP_'.curl_getinfo($resourceC->resource,
CURLINFO_EFFECTIVE_URL)),
));
######################OUTPUT#######################
Array
(
[0] => ResourceA Object
(
[resource] => Resource id #1
[id] => 934e1b0a64a6891959c68c254282d177
)
//This was affected by reference $resourceC->id = md5(rand());
[1] => RESOURCEPROP_2188ba855d89b48713a0dc4fecb2c8a9
)
Array
(
[0] => ResourceB Object
(
[resource] => Resource id #2
[id] => f11b5e72957e4d173f1ae86f34dd7f63
)
[1] => RESOURCEPROP_f11b5e72957e4d173f1ae86f34dd7f63
)
Array
(
[0] => ResourceA Object
(
//The resource remains the same (is not Resource id #3)
[resource] => Resource id #1
//curl_setopt($resourceC->resource, CURLOPT_URL, $resourceC->id);
[id] => 2188ba855d89b48713a0dc4fecb2c8a9
)
//$resourceC->id = md5(rand())
[1] => RESOURCEPROP_2188ba855d89b48713a0dc4fecb2c8a9
)
Expected result:
----------------
The resource should clone
$obj1->resource = Resource id #1
$obj2 = clone $obj
$obj2->resource = Resource id #2
Actual result:
--------------
The resource remains the same
$obj1->resource = Resource id #1
$obj2 = clone $obj
$obj2->resource = Resource id #1
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66116&edit=1