-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic_methods.php
More file actions
58 lines (48 loc) · 1.17 KB
/
magic_methods.php
File metadata and controls
58 lines (48 loc) · 1.17 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
<?php
class User {
private $data = [];
public function __get($name) {
if (isset($this->data[$name])) {
return $this->data[$name];
}
return null;
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function __isset($name) {
return isset($this->data[$name]);
}
public function __unset($name) {
unset($this->data[$name]);
}
public function __call($name, $arguments) {
if ($name === 'fullName') {
return $this->data['first_name'] . ' ' . $this->data['last_name'];
}
}
}
// Usage
$user = new User();
$user->first_name = 'John';
$user->last_name = 'Doe';
echo $user->first_name; // Output: John
echo "<br>";
echo $user->last_name; // Output: Doe
echo "<br>";
echo $user->age; // Output: null (property not set)
echo "<br>";
if (isset($user->first_name)) {
echo "First name is set";
} else {
echo "First name is not set";
}
echo "<br>";
unset($user->last_name);
if (isset($user->last_name)) {
echo "Last name is set";
} else {
echo "Last name is not set";
}
echo "<br>";
echo $user->fullName(); // Output: John Doe