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 => $data){ print_r($obj); }
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Creation of dynamic property Business::$staff is deprecated in /in/MTWIk on line 24 0

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
45.42 ms | 401 KiB | 8 Q