I'm confused on what a constructor looks like in PHP using a DDD aproach. This is what I have so far:
Entity
class People {
// Fields
private $id;
private $first_name; // required
private $middle_name;
private $last_name; // required
private $phone; // required (or mobile_phone required)
private $mobile_phone;
private $email; // required
private $alt_email;
private $something_else; // required
public function __construct($fields){
// Set some properties
$this->setFromArray($fields);
// Don't instantiate a new entity object in an invalid state
// (ie. determines if required fields are given)
if(!$this->isValid()){
throw new Exception("Can't create person");
}
}
// some getters and setters...
// some other domain methods so entity is not anemic
...
Repository
class PeopleRepository { // <-- Should probably be an interface
public function get($id){
...
}
public function save(People $people){
// Will INSERT or UPDATE based on if an ID is set in $people
}
Simple Example
// a very very simple example
$peopleRepo = new PeopleRepository();
$people = new People($_POST);
$peopleRepo->save($people);
I don't want to use any ORM. Is the way I do it above a correct approach for an entity constructor in DDD? Please explain and give example in PHP of how entity constructors look in DDD (I'm having a hard time finding good examples).