-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path14 All Magic Methods Overview.php
195 lines (165 loc) · 5.24 KB
/
14 All Magic Methods Overview.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
// We are implementing Serializable only for testing serialize() and unserialize()
class Car implements Serializable
{
public $name;
private $hidden = 'Serect';
// __construct ([ mixed $args = "" [, $... ]] ) : void
function __construct($name = null)
{
$this->name = $name;
echo ("Constructor is called\n");
}
public function say()
{
echo "My name is $this->name\n";
}
// __destruct ( void ) : void
function __destruct()
{
echo "Destroying " . __class__ . "\n";
}
// public __call ( string $name , array $arguments ) : mixed
public function __call($name, $arguments)
{
echo "Calling object method '$name', Arguments: " . implode(', ', $arguments) . "\n";
}
// public static __callStatic ( string $name , array $arguments ) : mixed
public static function __callStatic($name, $arguments)
{
echo "Calling static method '$name', Arguments: " . implode(', ', $arguments) . "\n";
}
// public __set ( string $name , mixed $value ) : void
public function __set($name, $value)
{
echo "Setting '$name' to '$value'\n";
$this->$name = $value;
}
// public __get ( string $name ) : mixed
public function __get($name)
{
echo "Getting '$name'\n";
return $this->$name;
}
// public __isset ( string $name ) : bool
public function __isset($name)
{
echo "Is '$name' set?\n";
return isset($this->$name);
}
// public __unset ( string $name ) : void
public function __unset($name)
{
echo "Unsetting '$name'\n";
unset($this->$name);
}
// public __sleep ( void ) : array
public function __sleep()
{
echo "It is invoked because serialize() method is invoked outside the class\n";
$this->name = base64_encode($this->name);
// should return an array
return array('name');
}
public function __wakeup()
{
echo "It is invoked when the unserialize() method is invoked outside the class.<br>";
$this->name = 'Mr. Bean';
// There is no need to return an array here.
}
// Serializable interface method
public function serialize()
{
return serialize('hello world');
}
// Serializable interface method
public function unserialize($serialized)
{
$this->name = unserialize($serialized);
}
// public __toString ( void ) : string
public function __toString()
{
return "Beautiful Car\n";
}
// __invoke ([ $... ] ) : mixed
public function __invoke($x = true)
{
var_dump($x);
}
// static __set_state ( array $properties ) : object
public static function __set_state($an_array)
{
$obj = new Car;
$obj->name = $an_array["name"];
return $obj;
}
// __debugInfo ( void ) : array
public function __debugInfo()
{
return ['name' => 'Debug Name'];
}
// __clone ( void ) : void
public function __clone()
{
echo "You are cloning the object\n";
}
}
// __construct is invoked
$bmw = new Car('X1');
// say() method from class is invoked
$bmw->say();
// class has no method names runTest() so __call is invoked
$bmw->runTest('Hi', 123);
// class has no static method names runTest() so __callStatic is invoked
Car::runTest('Hello', 123);
echo "\n";
// class has no private property 'aaa' so __set is invoked
$bmw->aaa = 1;
// class has private property 'hidden' so __set is invoked
$bmw->hidden = "Hacked";
// class has public property 'name' so __set is not invoked
$bmw->name = "X8";
echo "\n";
// class has no private property 'aaa' so __get is not invoked
echo $bmw->aaa . "\n";
// class has private property 'hidden' so __get is invoked
echo $bmw->hidden . "\n";
// class has public property 'name' so __get is invoked
echo $bmw->name . "\n";
echo "\n";
// class has private property 'hidden' so __isset is invoked
var_dump(isset($bmw->hidden));
// class has private property 'hidden' so __unset is invoked
unset($bmw->hidden);
var_dump(isset($bmw->hidden));
echo "\n";
// class has public property 'name' so __isset is not invoked
var_dump(isset($bmw->name));
// class has public property 'name' so __unset is not invoked
unset($bmw->name);
// Now 'name' property isn't set, that's why __isset is invoked to search it's private properties
var_dump(isset($bmw->name));
echo "\n";
// serialize will invoke __sleep method
echo (serialize($bmw));
// unserialize will invoke __wakeup method
// it will also invoke __destruct method
echo (unserialize(serialize($bmw)));
echo "\n";
// The __toString() method will be called when using echo method to print an object directly.
echo $bmw;
// When you try to call an object in the way of calling a function, the __invoke method will be called automatically.
echo $bmw();
echo "\n";
// __set_state() method is called by var_export()
$someCar = new Car('Some Name');
$someCar->name = "Honda";
eval('$b = ' . var_export($someCar, true) . ';');
echo var_export($b);
echo "\n";
// __debugInfo() method is called by var_dump() when dumping an object to get the properties that should be shown
var_dump(new Car("poll"));
echo "\n";
// __clone() method will be called when performing clone
$fromSomeCar = clone $someCar;