|
1 | 1 | <?php
|
2 |
| -class Car |
| 2 | +// We are implementing Serializable only for testing serialize() and unserialize() |
| 3 | +class Car implements Serializable |
3 | 4 | {
|
4 | 5 | public $name;
|
| 6 | + private $hidden = 'Serect'; |
5 | 7 |
|
6 | 8 | // __construct ([ mixed $args = "" [, $... ]] ) : void
|
7 | 9 | function __construct($name = null)
|
@@ -61,11 +63,133 @@ public function __unset($name)
|
61 | 63 | unset($this->$name);
|
62 | 64 | }
|
63 | 65 |
|
| 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 | + |
64 | 127 | }
|
65 | 128 |
|
66 |
| -// __construct |
67 |
| -// $bmw = new Car; |
| 129 | +// __construct is invoked |
68 | 130 | $bmw = new Car('X1');
|
| 131 | +// say() method from class is invoked |
69 | 132 | $bmw->say();
|
| 133 | +// class has no method names runTest() so __call is invoked |
70 | 134 | $bmw->runTest('Hi', 123);
|
| 135 | +// class has no static method names runTest() so __callStatic is invoked |
71 | 136 | 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