Skip to content

Commit cbe2d2d

Browse files
committed
update
1 parent afe282b commit cbe2d2d

File tree

1 file changed

+127
-3
lines changed

1 file changed

+127
-3
lines changed

files/14 All Magic Methods Overview.php

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2-
class Car
2+
// We are implementing Serializable only for testing serialize() and unserialize()
3+
class Car implements Serializable
34
{
45
public $name;
6+
private $hidden = 'Serect';
57

68
// __construct ([ mixed $args = "" [, $... ]] ) : void
79
function __construct($name = null)
@@ -61,11 +63,133 @@ public function __unset($name)
6163
unset($this->$name);
6264
}
6365

66+
// public __sleep ( void ) : array
67+
public function __sleep()
68+
{
69+
echo "It is invoked because serialize() method is invoked outside the class\n";
70+
$this->name = base64_encode($this->name);
71+
// should return an array
72+
return array('name');
73+
}
74+
75+
public function __wakeup()
76+
{
77+
echo "It is invoked when the unserialize() method is invoked outside the class.<br>";
78+
$this->name = 'Mr. Bean';
79+
// There is no need to return an array here.
80+
}
81+
82+
// Serializable interface method
83+
public function serialize()
84+
{
85+
return serialize('hello world');
86+
}
87+
88+
// Serializable interface method
89+
public function unserialize($serialized)
90+
{
91+
$this->name = unserialize($serialized);
92+
}
93+
94+
// public __toString ( void ) : string
95+
public function __toString()
96+
{
97+
return "Beautiful Car\n";
98+
}
99+
100+
// __invoke ([ $... ] ) : mixed
101+
public function __invoke($x = true)
102+
{
103+
var_dump($x);
104+
}
105+
106+
// static __set_state ( array $properties ) : object
107+
public static function __set_state($an_array)
108+
{
109+
$obj = new Car;
110+
$obj->name = $an_array["name"];
111+
112+
return $obj;
113+
}
114+
115+
// __debugInfo ( void ) : array
116+
public function __debugInfo()
117+
{
118+
return ['name' => 'Debug Name'];
119+
}
120+
121+
// __clone ( void ) : void
122+
public function __clone()
123+
{
124+
echo "You are cloning the object\n";
125+
}
126+
64127
}
65128

66-
// __construct
67-
// $bmw = new Car;
129+
// __construct is invoked
68130
$bmw = new Car('X1');
131+
// say() method from class is invoked
69132
$bmw->say();
133+
// class has no method names runTest() so __call is invoked
70134
$bmw->runTest('Hi', 123);
135+
// class has no static method names runTest() so __callStatic is invoked
71136
Car::runTest('Hello', 123);
137+
echo "\n";
138+
139+
// class has no private property 'aaa' so __set is invoked
140+
$bmw->aaa = 1;
141+
// class has private property 'hidden' so __set is invoked
142+
$bmw->hidden = "Hacked";
143+
// class has public property 'name' so __set is not invoked
144+
$bmw->name = "X8";
145+
echo "\n";
146+
147+
// class has no private property 'aaa' so __get is not invoked
148+
echo $bmw->aaa . "\n";
149+
// class has private property 'hidden' so __get is invoked
150+
echo $bmw->hidden . "\n";
151+
// class has public property 'name' so __get is invoked
152+
echo $bmw->name . "\n";
153+
echo "\n";
154+
155+
// class has private property 'hidden' so __isset is invoked
156+
var_dump(isset($bmw->hidden));
157+
// class has private property 'hidden' so __unset is invoked
158+
unset($bmw->hidden);
159+
var_dump(isset($bmw->hidden));
160+
echo "\n";
161+
162+
// class has public property 'name' so __isset is not invoked
163+
var_dump(isset($bmw->name));
164+
// class has public property 'name' so __unset is not invoked
165+
unset($bmw->name);
166+
// Now 'name' property isn't set, that's why __isset is invoked to search it's private properties
167+
var_dump(isset($bmw->name));
168+
echo "\n";
169+
170+
// serialize will invoke __sleep method
171+
echo(serialize($bmw));
172+
// unserialize will invoke __wakeup method
173+
// it will also invoke __destruct method
174+
echo(unserialize(serialize($bmw)));
175+
echo "\n";
176+
177+
// The __toString() method will be called when using echo method to print an object directly.
178+
echo $bmw;
179+
// When you try to call an object in the way of calling a function, the __invoke method will be called automatically.
180+
echo $bmw();
181+
echo "\n";
182+
183+
// __set_state() method is called by var_export()
184+
$someCar = new Car('Some Name');
185+
$someCar->name = "Honda";
186+
eval('$b = ' . var_export($someCar, true) . ';');
187+
echo var_export($b);
188+
echo "\n";
189+
190+
// __debugInfo() method is called by var_dump() when dumping an object to get the properties that should be shown
191+
var_dump(new Car("poll"));
192+
echo "\n";
193+
194+
// __clone() method will be called when performing clone
195+
$fromSomeCar = clone $someCar;

0 commit comments

Comments
 (0)