I have a Dbh class, a Person Class and a StudentCollection Class.

On my index.php page I include all 3 classes.

What I want to do is when I create a new StudentCollection instance, I want it to populate all my students automatically.

$StudentList = new StudentList();

Currently I'm doing this.

$StudentList = new StudentList->getStudents();

As my database query and loop is inside a getStudents public function.

The reason I want to do this is so I can have another method, findStudent which allows me to do something like this

$Student = $StudentList->findById(34);

Which will used the list created when StudentCollection was created.

    Weedpacket

    Working out the correct way of doing this

    This is what I've come up with

     class StudentCollection extends dbh {
        private array $ListArray= [];
        function __construct(){
            $this->ListArray= $this->getAllStudents();
        }
    .....

    getAllStudents then creates a DB search, returns an array.

    So later when I call my findById method it's go the ArrayList.

      Write a Reply...