php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66116 Cloning object leaving resource reference
Submitted: 2013-11-19 11:47 UTC Modified: 2013-11-19 18:56 UTC
From: joni2back at gmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: Irrelevant OS: Debian 6
Private report: No CVE-ID: None
 [2013-11-19 11:47 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-11-19 18:56 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2013-11-19 18:56 UTC] requinix@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC