3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A { public $x; public $y; public function __construct($x, $y) { $this->x = sha1($x); $this->y = $y; } public function __clone() { return new A($this->x, $this->y); } } class B extends A { public $z; public function __construct($z, $parent_obj) { $this->z = $z; parent::__construct($parent_obj->x, $parent_obj->y); } public function doSomeThing() { $parent_a_array = (array) parent::__clone(); var_dump($parent_a_array); $child_a_array = array_diff_assoc((array) $this, $parent_a_array); var_dump($child_a_array); echo "Parent Array Printing :\n"; foreach ($parent_a_array as $key => $value) { echo "Key: $key; Value: $value\n"; } echo "\nChild Array Printing: \n"; foreach ($child_a_array as $key => $value) { echo "Key: $key; Value: $value\n"; } } } $t = new B('C', new A("A", "B")); $t->doSomeThing();
Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
array(2) { ["x"]=> string(40) "726c6aeb8252ad589562fe2c7409d50c90a058aa" ["y"]=> string(1) "B" } array(2) { ["x"]=> string(40) "bd605412133b28b10c5fa7a45fce29df67c18bd7" ["z"]=> string(1) "C" } Parent Array Printing : Key: x; Value: 726c6aeb8252ad589562fe2c7409d50c90a058aa Key: y; Value: B Child Array Printing: Key: x; Value: bd605412133b28b10c5fa7a45fce29df67c18bd7 Key: z; Value: C
Output for 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
array(2) { ["x"]=> string(40) "726c6aeb8252ad589562fe2c7409d50c90a058aa" ["y"]=> string(1) "B" } array(2) { ["z"]=> string(1) "C" ["x"]=> string(40) "bd605412133b28b10c5fa7a45fce29df67c18bd7" } Parent Array Printing : Key: x; Value: 726c6aeb8252ad589562fe2c7409d50c90a058aa Key: y; Value: B Child Array Printing: Key: z; Value: C Key: x; Value: bd605412133b28b10c5fa7a45fce29df67c18bd7

preferences:
155.81 ms | 403 KiB | 187 Q