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: 2020-02-28 14:43 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: nikic (profile)
Status: Closed Package: Reflection related
PHP Version: trunk-SVN-2011-02-09 (snap) OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: whatthejeff at gmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

Pull requests:

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.
 [2019-08-04 05:40 UTC] trannguyenhoang8433 at gmail dot com
The following pull request has been associated:

Patch Name: protect master branches except for the pecl repos against force pushes
On GitHub:  https://github.com/php/karma/pull/4
Patch:      https://github.com/php/karma/pull/4.patch
 [2019-08-04 05:41 UTC] trannguyenhoang8433 at gmail dot com
The following pull request has been associated:

Patch Name: error message should matched path
On GitHub:  https://github.com/php/pecl-database-pdo_informix/pull/1
Patch:      https://github.com/php/pecl-database-pdo_informix/pull/1.patch
 [2020-02-28 14:43 UTC] nikic@php.net
-Status: Analyzed +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-02-28 14:43 UTC] nikic@php.net
isCloneable() for SplFileObject/SplTempFileObject in particular has been fixed in bug #72884.

Fact remains though that the final arbiter is whether `clone $obj` does not throw, isCloneable() can only help to detect cases that are definite not cloneable.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 16:01:29 2024 UTC