Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实体';在php中DDD的构造函数看起来像什么?_Php_Constructor_Entity_Domain Driven Design_Ddd Repositories - Fatal编程技术网

实体';在php中DDD的构造函数看起来像什么?

实体';在php中DDD的构造函数看起来像什么?,php,constructor,entity,domain-driven-design,ddd-repositories,Php,Constructor,Entity,Domain Driven Design,Ddd Repositories,我对使用DDD aproach的PHP中的构造函数是什么样子感到困惑。这就是我到目前为止所做的: 实体 class People { // Fields private $id; private $first_name; // required private $middle_name; private $last_name; // required private $phone; // require

我对使用DDD aproach的PHP中的构造函数是什么样子感到困惑。这就是我到目前为止所做的:

实体

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
    ...
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
    }
存储库

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
    ...
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
    }

我不想使用任何ORM。对于DDD中的实体构造函数来说,我在上面这样做的方法是正确的吗?请解释并用PHP举例说明实体构造函数在DDD中的外观(我很难找到好的例子)

在构造函数上传递值数组不是一个好主意。如果阵列中不存在所需的数据。您的域实体将处于无效状态

只需将所需字段分别放在构造函数上,这样它更具可读性,并且所需字段是显式的。如果您忘记提供所需的数据,则会出现一个错误,这是大多数优秀IDE所支持的

__construct($firstName, $lastName, $phone, $email) { }

您可能还想考虑使用ValueObjt对相关数据进行分组以缩小构造函数。有关ValueObjects的更多信息,请访问以下链接

以你的名字为例。将它们封装在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 ONLY
    // you need to instantiate new FullName if you want to change the fields
}
然后您可以将其传递给
People

__construct(FullName $fullName, $phone, $email) { }

如果你真的有巨大的构造函数,你可以考虑构造器模式。< /P>


是的!我的结论就是这样。我本来打算最终回答我自己的问题,但你抢先回答了。事实上,我现在经常使用值对象,而且进展很顺利。非常感谢。接受这个…很乐意帮忙:)坚持下去!