2

I've been developing my own framework from scratch until there are column updates here and there so I need to add some repetitive additional code lines to cast the database object in a model.

class Student
{
  public $id;
  public $name;

  public function setById($id) {
     ... // PDO $stmt

     // Manual casting works fine but not easy to maintain
     // $student = $stmt->fetchObject();
     // $this->id = $student->id;
     // $this->name = $student->name;

     // So, I'm trying to do auto cast, but $this cannot be re-assign.
     $stmt->setFetchMode(PDO::FETCH_CLASS, 'Student');
     $this = $stmt->fetch(); // Error: Cannot re-assign $this
  }
}

Is there any native syntax to do this or it should be done by custom function?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Jeaf Gilbert
  • 11,495
  • 19
  • 78
  • 105

0 Answers0