Bug #53967 [Ana]: ReflectionClass::isCloneable reports false positives
| From: | nikic@php.net | Date: | Sat, 09 Dec 2017 22:03:08 +0000 |
| Subject: | Bug #53967 [Ana]: ReflectionClass::isCloneable reports false positives | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-213036@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=53967&edit=1
ID: 53967
Updated by: nikic@php.net
Reported by: whatthejeff at gmail dot com
Summary: ReflectionClass::isCloneable reports false positives
Status: Analyzed
Type: Bug
Package: Reflection related
PHP Version: trunk-SVN-2011-02-09 (snap)
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2017-12-07 13:00:21] sebastian@php.net
Is there any chance to fix ReflectionClass::isCloneable() to work correctly for all internal
classes?
------------------------------------------------------------------------
[2011-02-11 22:48:53] 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-02-11 22:43:57] 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-09 07:47:43] 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-09 04:30:41] 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)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=53967&edit=1