-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_object_cloning.php
More file actions
49 lines (37 loc) · 989 Bytes
/
class_object_cloning.php
File metadata and controls
49 lines (37 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/*
* Copyright (C) 2024-2026 Katarzyna Krasińska
* PHP.lab - https://github.com/katheroine/php.lab
* Licensed under GPL-3.0 - see LICENSE.md
*/
class SomeClass
{
public string $someRegularProperty;
public OtherClass $someObjectProperty;
public function __construct()
{
$this->someRegularProperty = 'original';
$this->someObjectProperty = new OtherClass();
}
}
class OtherClass
{
public string $someProperty = 'original';
}
$someObject = new SomeClass();
var_dump($someObject);
print(PHP_EOL);
$otherObject = clone $someObject;
var_dump($otherObject);
print(PHP_EOL);
print(
'Equal: ' . ($someObject == $otherObject ? 'yes' : 'no') . PHP_EOL
. 'Identical: ' . ($someObject === $otherObject ? 'yes' : 'no') . PHP_EOL
. PHP_EOL
);
$someObject->someRegularProperty = 'modified';
$someObject->someObjectProperty->someProperty = 'modified';
var_dump($someObject);
print(PHP_EOL);
var_dump($otherObject);
print(PHP_EOL);