|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2013-11-19 18:56 UTC] requinix@php.net
-Status: Open
+Status: Not a bug
[2013-11-19 18:56 UTC] requinix@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 16:00:01 2025 UTC |
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