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

preferences:
140.33 ms | 404 KiB | 5 Q