3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A { public function do_something() { echo "class A did something"; } } class B { private $arr = array(); private $current_index = 0; public function add_new_A() { $new_a = new A; $this->arr[$this->current_index] = $new_a; $this->current_index++; } public function get_an_A_by_index($index) { return $this->arr[$index]; } public function do_something_with_A_member_inside_array($index) { self::get_an_A_by_index($index)->do_something(); } } $b = new B; $b->add_new_a(); $b->add_new_a(); echo print_r($b->get_an_A_by_index(0)); echo "\n"; $b->do_something_with_A_member_inside_array(0); // returns error
Output for 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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
A Object ( ) 1 class A did something

preferences:
145.16 ms | 407 KiB | 5 Q