PHP 面向对象编程与加密技术全解析
1. 对象克隆
在 PHP 里,对象克隆是创建对象副本的方式。以下代码展示了对象克隆及相关比较:
$clone = clone $parent;
if ($parent === $clone) {
echo 'The parent and clone are the same object!' . PHP_EOL;
}
if ($parent == $clone) {
echo 'The parent and clone have the same data!' . PHP_EOL;
}
if ($parent->child === $clone->child) {
echo 'The parent and the clone have the same child!' . PHP_EOL;
}
if ($parent->child == $clone->child) {
echo 'The parent and the clone have the same child data!' . PHP_EOL;
这里, $parent
和 $clone
是不同的引用,所以 $parent === $clone
为 false
;但它们数据相同, $parent == $clone
为 true
。