I'm new to OOP PHP and have been following Laracasts OOP Bootcamp. I have reached a certain part of the tutorial where I'm having trouble. I have the following file (ignore that conventions were not followed, as it is the learning phase):
<?php
class Person
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
}
class Staff
{
protected $members = [];
public function __constructor($members = [])
{
$this->members = $members;
}
public function add(Person $person)
{
$this->members[] = $person;
}
public function members()
{
return $this->members;
}
}
class Business
{
protected $staff;
public function __construct(Staff $staff)
{
$this->staff = $staff;
}
public function hire(Person $person)
{
$this->staff->add($person);
}
public function getStaffMembers()
{
return $this->staff->members();
}
}
$jeffrey = new Person('Jeffrey Way');
$staff = new Staff([$jeffrey]);
$laracasts = new Business($staff);
$laracasts->hire(new Person('Jane Doe'));
var_dump($laracasts->getStaffMembers());
Unfortunately, the var_dump is only giving me one staff member:
array(1) {
[0]=>
object(Person)#4 (1) {
["name":protected]=>
string(8) "Jane Doe"
}
}
I have tried adding []
to the line $this->members = $members;
under Staff class but it's still giving me the same output. I've also double checked the files, I should be expecting two members instead of one.
Can someone tell me where I went wrong?
Thanks for your time.