3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Provides dependency injection friendly methods for serialization. */ trait DependencySerializationTrait { /** * {@inheritdoc} */ public function __sleep() { echo "hydrating " . $this::class . PHP_EOL; $closure = \Closure::bind(static function ($class) { return get_object_vars($class); }, NULL, $this); $vars = $closure($this); return array_keys($vars); } /** * {@inheritdoc} */ #[\ReturnTypeWillChange] public function __wakeup() { echo "dehydrating " . $this::class . PHP_EOL; } } trait dumper { public function doIt() { print "get_object_vars()\n"; print_r(get_object_vars($this)); $closure = \Closure::bind(static function ($class) { return get_object_vars($class); }, NULL, $this); $vars = $closure($this); print "\n\nClosure\n"; print_r($vars); } } class A { use dumper; use DependencySerializationTrait; private int $a = 1; public function echoA() { echo $this::class . "::" . __METHOD__ . ":" . $this->a . PHP_EOL; } } class B extends A { } class C extends B { use dumper; private int $a = 2; public function scopeCechoA() { echo $this::class . "::" . __METHOD__ . ":" . $this->a . PHP_EOL; } } class D extends B { use DependencySerializationTrait; private int $a = 3; public function scopeDechoA() { echo $this::class . "::" . __METHOD__ . ":" . $this->a . PHP_EOL; } } print "Class A\n"; (new A())->doit(); print "\n\nClass B\n"; $b = new B(); ($b)->doit(); print "\n\nClass C\n"; $c = new C(); ($c)->doit(); print "\n\nClass D\n"; $d = new D(); ($d)->doit(); echo PHP_EOL . PHP_EOL; echo ">>>>>>>>" . PHP_EOL; echo "Before B got serialized/deserialized:" . PHP_EOL; $b->echoA(); $b2 = unserialize(serialize($b)); echo "After B got serialized/deserialized:" . PHP_EOL; $b2->echoA(); echo "<<<<<<<<" . PHP_EOL; echo PHP_EOL . PHP_EOL; echo ">>>>>>>>" . PHP_EOL; echo "Before C got serialized/deserialized:" . PHP_EOL; $c->echoA(); $c->scopeCechoA(); $c2 = unserialize(serialize($c)); echo "After C got serialized/deserialized:" . PHP_EOL; $c2->echoA(); $c2->scopeCechoA(); echo "<<<<<<<<" . PHP_EOL; echo PHP_EOL . PHP_EOL; echo ">>>>>>>>" . PHP_EOL; echo "Before D got serialized/deserialized:" . PHP_EOL; $d->echoA(); $d->scopeDechoA(); $d2 = unserialize(serialize($d)); echo "After D got serialized/deserialized:" . PHP_EOL; $d2->echoA(); $d2->scopeDechoA(); echo "<<<<<<<<" . PHP_EOL;

preferences:
40.49 ms | 408 KiB | 5 Q