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;
Output for git.master_jit, git.master, rfc.property-hooks
Class A get_object_vars() Array ( [a] => 1 ) Closure Array ( [a] => 1 ) Class B get_object_vars() Array ( [a] => 1 ) Closure Array ( ) Class C get_object_vars() Array ( [a] => 2 ) Closure Array ( [a] => 2 ) Class D get_object_vars() Array ( [a] => 1 ) Closure Array ( [a] => 3 ) >>>>>>>> Before B got serialized/deserialized: B::A::echoA:1 hydrating B dehydrating B After B got serialized/deserialized: B::A::echoA:1 <<<<<<<< >>>>>>>> Before C got serialized/deserialized: C::A::echoA:1 C::C::scopeCechoA:2 hydrating C dehydrating C After C got serialized/deserialized: C::A::echoA:1 C::C::scopeCechoA:2 <<<<<<<< >>>>>>>> Before D got serialized/deserialized: D::A::echoA:1 D::D::scopeDechoA:3 hydrating D dehydrating D After D got serialized/deserialized: D::A::echoA:1 D::D::scopeDechoA:3 <<<<<<<<

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
43.23 ms | 407 KiB | 5 Q