Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TuqueroIvantckJernalyn's avatar

array not added to PHP class' property

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):

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?

0 likes
3 replies
Sinnbeck's avatar

Can you format your code by adding ``` on the line before and after the code.

Can you also show the staff class? My guess the error is in that

rodrigo.pedra's avatar

Can you post the full Staff and Person classes?

By the way, to pretty format your code, so it is easier to follow, wrap it into three backticks (`), or three tilde (~) characters:

~~~php
echo 'hello world';
~~~

As you can guess, you can give a hint on which language you are using on the opening sequence.

Please or to participate in this conversation.