-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.php
More file actions
43 lines (33 loc) · 1.07 KB
/
demo.php
File metadata and controls
43 lines (33 loc) · 1.07 KB
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
<?php
require_once 'bootstrap.php';
use AJBnet\Core\Traits\MutableObject;
use AJBnet\Core\Traits\ImmutableObject;
echo "=== Demonstrating MutableObject vs ImmutableObject ===\n\n";
// MutableObject example
class MutableExample {
use MutableObject;
}
echo "MutableObject Example:\n";
$mutable = new MutableExample();
$mutable->name = "Initial Name";
echo "Initial: name = {$mutable->name}\n";
$mutable->name = "Changed Name";
echo "After change: name = {$mutable->name}\n";
echo "✓ Mutable object allows property changes\n\n";
// ImmutableObject example
class ImmutableExample {
use ImmutableObject;
}
echo "ImmutableObject Example:\n";
$immutable = new ImmutableExample();
$immutable->name = "Initial Name";
echo "Initial: name = {$immutable->name}\n";
try {
$immutable->name = "Changed Name";
echo "ERROR: Should not reach this line\n";
} catch (InvalidArgumentException $e) {
echo "Attempted change failed: {$e->getMessage()}\n";
echo "Final: name = {$immutable->name}\n";
echo "✓ Immutable object prevents property changes\n";
}
echo "\n=== Demonstration complete ===\n";