International PHP Conference Berlin 2023

ReflectionClass::isCloneable

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

ReflectionClass::isCloneable返回了一个类是否可复制

说明

public ReflectionClass::isCloneable(): bool

返回了这个类是否可复制。

参数

此函数没有参数。

返回值

如果这个类可以复制返回 true,否则返回 false

范例

示例 #1 ReflectionClass::isCloneable() 的基本用法

<?php
class NotCloneable {
    public 
$var1;
    
    private function 
__clone() {
    }
}

class 
Cloneable {
    public 
$var1;
}

$notCloneable = new ReflectionClass('NotCloneable');
$cloneable = new ReflectionClass('Cloneable');

var_dump($notCloneable->isCloneable());
var_dump($cloneable->isCloneable());
?>

以上例程会输出:

bool(false)
bool(true)

add a note

User Contributed Notes 4 notes

up
1
xxxargonxxx at gmail dot com
3 years ago
I wonder how this method decides of is it clonable or not. There is no explanation.
up
1
info at ensostudio dot ru
3 months ago
similar function:
<?php
function isCloneable(object $obj): bool
{
       return !
method_exists($obj, '__clone') || is_callable([$obj, '__clone']);
}
?>
up
0
info at ensostudio dot ru
3 months ago
this method checks is  `__clone()`  method was declared as private
up
-4
php at abiusx dot com
6 years ago
This does not work for many of core-classes, just like most other reflection methods.
To Top