3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Person { protected $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } } class Business { // adding Staff class to Business public function __construct(Staff $staff) { $this->staff = $staff; } // manual hire(adding Person to Staff) public function hire(Person $person) { // add to staff $this->staff->add($person); } // fetch members public function getStaffMembers() { return $this->staff->members(); } } class Staff { // adding people from Person class to "member" variable protected $members = []; public function __construct($members = []) { $this->members = $members; } // adding person to members public function add(Person $person) { $this->members[] = $person; } public function members() { return $this->members; } } // you can also create an array with this method $bros = [ 'Bro', 'Zdenko', 'Miljan', 'Kesten' ]; // pretty simple to understand this part $employees = new Person([$bros]); $staff = new Staff([$employees]); $business = new Business($staff); //var_dump($business->getStaffMembers()); // or the print_r, it doesn't matter //print_r($business->getStaffMembers()); /* You have an array of members now, if you'd like to utilize this array * you could iterate over it and echo each member, within a foreach loop * */ $membersArray = $business->getStaffMembers(); foreach($membersArray as $obj){ print_r($obj); }
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Creation of dynamic property Business::$staff is deprecated in /in/VAcLl on line 24 Person Object ( [name:protected] => Array ( [0] => Array ( [0] => Bro [1] => Zdenko [2] => Miljan [3] => Kesten ) ) )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Deprecated: Creation of dynamic property Business::$staff is deprecated in /in/VAcLl on line 24 Person Object ( [name:protected] => Array ( [0] => Array ( [0] => Bro [1] => Zdenko [2] => Miljan [3] => Kesten ) ) )
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 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
Person Object ( [name:protected] => Array ( [0] => Array ( [0] => Bro [1] => Zdenko [2] => Miljan [3] => Kesten ) ) )

preferences:
211.07 ms | 404 KiB | 330 Q