0
我是OOP PHP的新手,一直关注Laracasts OOP Bootcamp。我遇到了麻烦的教程的某个部分。我有以下文件(忽略公约没有得到遵守,因为它是学习阶段):为什么数组未添加到PHP类的属性?
<?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());
不幸的是,后续代码var_dump只给我一个工作人员:
array(1) {
[0]=>
object(Person)#4 (1) {
["name":protected]=>
string(8) "Jane Doe"
}
}
我曾尝试将[]
添加到Staff类下的$this->members = $members;
行,但它仍然给我输出相同的结果。我也加倍检查了这些文件,我应该期待两个成员而不是一个。
有人能告诉我我哪里出错了吗?
谢谢你的时间。