I have a problem with the following code, I am trying to print the data of a student but I get the following error:
Fatal error: Uncaught Error: Cannot access private property Secretary::$students
If i put the attributes as public, it works correctly.
class Student {
private $name;
public function __construct($name){
$this->name = $name;
}
public function getName(){
return $this->name;
}
}
class Secretary{
private $students = array();
public function printStudents(){
foreach($this->students as $key=>$value){
echo $value->name . " ";
}
}
}
$secretary = new Secretary();
// Add students.
$student = new student("Student1");
array_push($secretary->students,$student);
$secretary->printStudents();