3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A { public $x; public $y; public function __construct($x, $y) { $this->x = ($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 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array(2) { ["x"]=> string(1) "A" ["y"]=> string(1) "B" } array(1) { ["z"]=> string(1) "C" } Parent Array Printing : Key: x; Value: A Key: y; Value: B Child Array Printing: Key: z; Value: C

preferences:
221.35 ms | 404 KiB | 238 Q