-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertiesOverLoading.php
More file actions
69 lines (54 loc) · 1.78 KB
/
Copy pathPropertiesOverLoading.php
File metadata and controls
69 lines (54 loc) · 1.78 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
69
<?php
/**
* overloading properties adalah kemampuan membuat propertie jika properti tersebut tidak ada di dalam class yang dipakai,
* overloading bisa digunakan ketika get (memanggil), set (membuat), isset (mengecek), unset (mengundo/menghapus)
* intinya jadi dinamis, kalau gk ada dia dibuat, dan nantinya bisa dipanggil jika disimpannya dalam array
*/
class Zero
{
private array $properties = [];
// ini properties magic function, tidak ada properties tapi di panggil.
public function __get($name)
{
return $this->properties[$name];
// echo "mengambil properties $name" . PHP_EOL;
}
public function __set($name, $value)
{
return $this->properties[$name] = $value;
// echo "membuat properties $name, dengan data $value" . PHP_EOL;
}
public function __isset($name): bool
{
return isset($this->properties[$name]);
// echo "Isset $name";
// return false;
}
public function __unset($name)
{
unset($this->properties[$name]);
// echo "Unset $name";
}
/**
* separator ============================================================== separator
*/
// ini properties fungtion, tidak ada function tapi di panggil.
public function __call($name, $arguments)
{
$join = join(",", $arguments);
echo "Call function $name with arguments $join" . PHP_EOL;
}
public static function __callStatic($name, $arguments)
{
$join = join(",", $arguments);
echo "Call static function $name with arguments $join" . PHP_EOL;
}
}
$zero = new Zero();
$zero->firstName = "galih";
$zero->lastName = "rizki";
echo $zero->firstName . PHP_EOL;
echo $zero->lastName . PHP_EOL;
var_dump($zero);
$zero->sayHello("galih");
Zero::sayHello("rzk");