@@ -138,7 +138,12 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
138138 /** @param array<mixed[]|object> $parents */
139139 private function dumpObject (object $ var , array $ parents , int $ level ): string
140140 {
141- if ($ var instanceof \Serializable) {
141+ $ class = $ var ::class;
142+ if (method_exists ($ var , '__serialize ' )) {
143+ $ data = $ this ->dump ($ var ->__serialize ());
144+ return '\\' . self ::class . "::createObject( \\$ class::class, $ data) " ;
145+
146+ } elseif ($ var instanceof \Serializable) { // deprecated
142147 return 'unserialize( ' . $ this ->dumpString (serialize ($ var )) . ') ' ;
143148
144149 } elseif ($ var instanceof \UnitEnum) {
@@ -155,7 +160,6 @@ private function dumpObject(object $var, array $parents, int $level): string
155160 throw new Nette \InvalidArgumentException ('Cannot dump closure. ' );
156161 }
157162
158- $ class = $ var ::class;
159163 if ((new \ReflectionObject ($ var ))->isAnonymous ()) {
160164 throw new Nette \InvalidArgumentException ('Cannot dump anonymous class. ' );
161165
@@ -270,6 +274,11 @@ private function dumpArguments(array &$var, int $column, bool $named): string
270274 */
271275 public static function createObject (string $ class , array $ props ): object
272276 {
277+ if (method_exists ($ class , '__serialize ' )) {
278+ $ obj = (new \ReflectionClass ($ class ))->newInstanceWithoutConstructor ();
279+ $ obj ->__unserialize ($ props );
280+ return $ obj ;
281+ }
273282 return unserialize ('O ' . substr (serialize ($ class ), 1 , -1 ) . substr (serialize ($ props ), 1 ));
274283 }
275284}
0 commit comments