php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53967 ReflectionClass::isCloneable reports false positives
Submitted: 2011-02-09 04:30 UTC Modified: 2017-12-09 22:03 UTC
Votes:5
Avg. Score:3.4 ± 1.0
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: whatthejeff at gmail dot com Assigned:
Status: Analyzed Package: Reflection related
PHP Version: trunk-SVN-2011-02-09 (snap) OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-02-09 04:30 UTC] whatthejeff at gmail dot com
Description:
------------
ReflectionClass::isCloneable returns true on some classes which are not actually 
cloneable.

Test script:
---------------
<?php

$reflection = new ReflectionClass('SplFileObject');
var_dump($reflection->isCloneable());

$reflection = new ReflectionClass('SplTempFileObject');
var_dump($reflection->isCloneable());

?>

Expected result:
----------------
bool(false)
bool(false)

Actual result:
--------------
bool(true)
bool(true)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-09 07:47 UTC] sebastian@php.net
I do not think that this is limited to SplFileObject. Have a look at

sb@thinkpad ~ % php -r'print_r(array_filter(get_declared_classes(),function($n){$c=new ReflectionClass($n);return $c->isCloneable();}));'
Array
(
    [0] => stdClass
    [3] => Closure
    [4] => DateTime
    [5] => DateTimeZone
    [6] => DateInterval
    [7] => DatePeriod
    [8] => LibXMLError
    [13] => DOMStringList
    [14] => DOMNameList
    [15] => DOMImplementationList
    [16] => DOMImplementationSource
    [17] => DOMImplementation
    [18] => DOMNode
    [19] => DOMNameSpaceNode
    [20] => DOMDocumentFragment
    [21] => DOMDocument
    [22] => DOMNodeList
    [23] => DOMNamedNodeMap
    [24] => DOMCharacterData
    [25] => DOMAttr
    [26] => DOMElement
    [27] => DOMText
    [28] => DOMComment
    [29] => DOMTypeinfo
    [30] => DOMUserDataHandler
    [31] => DOMDomError
    [32] => DOMErrorHandler
    [33] => DOMLocator
    [34] => DOMConfiguration
    [35] => DOMCdataSection
    [36] => DOMDocumentType
    [37] => DOMNotation
    [38] => DOMEntity
    [39] => DOMEntityReference
    [40] => DOMProcessingInstruction
    [41] => DOMStringExtend
    [42] => DOMXPath
    [43] => finfo
    [70] => EmptyIterator
    [72] => ArrayObject
    [73] => ArrayIterator
    [74] => RecursiveArrayIterator
    [75] => SplFileInfo
    [76] => DirectoryIterator
    [77] => FilesystemIterator
    [78] => RecursiveDirectoryIterator
    [79] => GlobIterator
    [80] => SplFileObject
    [81] => SplTempFileObject
    [82] => SplDoublyLinkedList
    [83] => SplQueue
    [84] => SplStack
    [86] => SplMinHeap
    [87] => SplMaxHeap
    [88] => SplPriorityQueue
    [89] => SplFixedArray
    [90] => SplObjectStorage
    [91] => MultipleIterator
    [93] => PDO
    [94] => PDOStatement
    [97] => Reflection
    [107] => __PHP_Incomplete_Class
    [108] => php_user_filter
    [109] => Directory
    [110] => SimpleXMLElement
    [111] => SimpleXMLIterator
    [112] => SoapClient
    [113] => SoapVar
    [114] => SoapServer
    [116] => SoapParam
    [117] => SoapHeader
    [119] => Phar
    [120] => PharData
    [121] => PharFileInfo
    [124] => XMLReader
)

I am sure that there are more false positives in there.
 [2011-02-11 22:43 UTC] felipe@php.net
SplFileObject and SplTempFileObject are exception because the way as it was coded, there is a logic behinds it to decide if it is clonable or not...
 [2011-02-11 22:48 UTC] whatthejeff at gmail dot com
Yeah, I actually realized ReflectionClass::isCloneable wouldn't work for SplFileObject and SplTempFileObject when I was looking at the implementation for 
SplFileObject.
 [2011-03-08 16:28 UTC] felipe@php.net
-Status: Open +Status: Analyzed
 [2017-12-07 13:00 UTC] sebastian@php.net
Is there any chance to fix ReflectionClass::isCloneable() to work correctly for all internal classes?
 [2017-12-09 22:03 UTC] nikic@php.net
I don't think so. As was mentioned, some objects determine clonability dynamically. There is no real way to determine if something is clonable short of doing it and seeing if it throws. This also holds for userland objects, of course. isClonable() can tell you when something definitely cannot be cloned, but it's just impossible to say if it will succeed once __clone() comes into the picture.

What do you need it for anyway? As of PHP 7, just cloning the object and catching Errors should be a fine way to determine clonability, as it doesn't throw a fatal anymore.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Mar 24 06:01:26 2019 UTC