Best way to do multiple constructors in PHP

You can’t put two __construct functions with unique argument signatures in a PHP class. I’d like to do this:

class Student 
{
   protected $id;
   protected $name;
   // etc.

   public function __construct($id){
       $this->id = $id;
      // other members are still uninitialized
   }

   public function __construct($row_from_database){
       $this->id = $row_from_database->id;
       $this->name = $row_from_database->name;
       // etc.
   }
}

What is the best way to do this in PHP?

25 Answers
25

Leave a Comment