0

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.

1 Answer 1

1

Your code is correct except for one line - the constructor for the class Staff is defined incorrectly. Replace

public function __constructor($members = [])

with

public function __construct($members = [])

Because of this mistake, the line $staff = new Staff([$jeffrey]); has no effect to the initialization of your internal $members array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.