У меня был один урок в ООП, который включал обмен сообщениями между классами. В учебнике парень просто показал выходную версию var_dump. Я хотел сыграть с кодом и перейти от var_dump к эхо-результату, потому что это было бы мне более полезно в будущем. Я просто не мог найти решение, чтобы вы, ребята, были моим единственным вариантом. Вот код.echo OOP php method
<?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());
?>
В чем ваш вопрос или проблема? PS: Вы не можете эхо-массива, вы получите только 'Array'. –
Начните с руководства по массивам http://php.net/manual/en/language.types.array.php –
Ваша реализация этих классов классов неверна - вы передаете массивы, в которых классы ожидают, что отдельные объекты –