-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_object_modifying.php
More file actions
68 lines (51 loc) · 1.44 KB
/
class_object_modifying.php
File metadata and controls
68 lines (51 loc) · 1.44 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?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
{
function __construct(
public mixed $publicProperty,
protected string $protectedProperty,
private string $privateProperty = 'nothing',
) {
}
public function setProtectedProperty(string $protectedProperty)
{
$this->protectedProperty = 'base ' . $protectedProperty;
}
public function setPrivateProperty(string $privateProperty)
{
$this->privateProperty = 'base ' . $privateProperty;
}
}
class OtherClass extends SomeClass
{
public function setProtectedProperty(int|string $protectedProperty)
{
$this->protectedProperty = 'derived ' . $protectedProperty;
}
}
$someObject = new SomeClass('some value', 15.5);
print("Some object:\n\n");
print_r($someObject);
print(PHP_EOL);
$someObject->publicProperty = 'orange';
$someObject->setProtectedProperty('tangerine');
$someObject->setPrivateProperty(1024);
print_r($someObject);
print(PHP_EOL);
$someObject->someDynamicProperty = '16';
$someObject->otherDynamicProperty = 'coffee';
print_r($someObject);
print(PHP_EOL);
$otherObject = new OtherClass('other value', 10);
print("Other object:\n\n");
print_r($otherObject);
print(PHP_EOL);
$otherObject->publicProperty = 100;
$otherObject->setProtectedProperty(200);
print_r($otherObject);
print(PHP_EOL);