Code: Select all
class Section {
...
private $idSection;
private $students; //array of objects from the Student class
public function __construct($datas){
foreach($datas as $key=> $value) {
$this->$key= $value;
}
if(empty($datas['students'])) {
$pdo = new PDO(...);
$req = "SELECT *
FROM student
WHERE idSection = :idSection ";
$stmt = $pdo->prepare($req);
$stmt->execute(array("idSection" => $this->idSection));
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$students= $stmt->fetchAll();
foreach($students as $student){
$this->students[] = new Student($student);
}
}
}
}
class Student {
...
private $idSection;
private $section; //object from the Section class
public function __construct($datas){
foreach($datas as $key=> $value) {
$this->$key= $value;
}
if(empty($datas ['section'])) {
$pdo = new PDO(...);
$req = "SELECT *
FROM section
WHERE idSection = :idSection ";
$stmt = $pdo->prepare($req);
$stmt->execute(array("idSection" => $this->idSection));
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$section= $stmt->fetch();
$this->section= new Section($section);
}
}
}
- Der Konstruktor des Abschnitts findet die Liste der Schüler in der Datenbank, dann nutzt die Konstruktorin des Schülers eine Schülerin, um ein Schüler zu erstellen. $this->students
- the constructor of the Student class finds the section in the database, then uses the constructor of the Section class to create a Section object and then, assign it to the $this->section
Wenn ich diesen" außerhalb der Konstruktoren "versuche (d. H. Untersuchung dieser Informationen, bevor ich entweder einen Abschnitt oder ein Studentobjekt erstellt und sie an die Konstrukteure übergeben kann), zeigt