What does an entity's constructor look like for DDD in php? -
i'm confused on constructor looks in php using ddd aproach. have 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 properties $this->setfromarray($fields); // don't instantiate new entity object in invalid state // (ie. determines if required fields given) if(!$this->isvalid()){ throw new exception("can't create person"); } } // getters , setters... // other domain methods entity not anemic ... repository
class peoplerepository { // <-- should interface public function get($id){ ... } public function save(people $people){ // insert or update based on if id set in $people } simple example
// very simple example $peoplerepo = new peoplerepository(); $people = new people($_post); $peoplerepo->save($people); i don't want use orm. way above correct approach entity constructor in ddd? please explain , give example in php of how entity constructors in ddd (i'm having hard time finding examples).
passing array of values on constructor not idea. if required data not present in array. domain entity in invalid state.
just put required fields individually on constructor, way more readable , required fields explicit. if forgot provide required data, have error supported ides.
__construct($firstname, $lastname, $phone, $email) { } you might want consider using valueobject group related data narrow down constructor. more information valueobjects follow links.
http://richardmiller.co.uk/2014/11/06/value-objects/
in case name. encapsulate them inside valueobject fullname
final class fullname { private $firstname; private $middlename; private $lastname; public function __construct($firstname, $lastname, $middlename = null) { $this->firstname = $firstname; $this->lastname = $lastname; $this->middlename = $middlename; } // getters method // need instantiate new fullname if want change fields } then can pass constructor of people
__construct(fullname $fullname, $phone, $email) { } if have huge constructor, might consider builder pattern.
Comments
Post a Comment