G
G
gog692016-07-29 16:07:20
PHP
gog69, 2016-07-29 16:07:20

What pattern is needed here?

An object is created based on some data, and in the process, it is possible that a certain property should receive the value of a new instance of the same
type class

class Detail {
     function create($data) {
          ......
         
         $this->nestedDatail = new Detail()->create( $data->nestedDetailData );
         return $this;
     }
}

(the constructor is busy with dependency injection)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Koryukov, 2016-07-29
@MadridianFox

Is a patch needed here? Perhaps it would be worth describing the shortcomings of what is, Wishlist, and ask how to achieve this.

A
Andrey Kulikovsky, 2016-07-30
@by25

Your class is very similar to a business logic object (domain entity).
It is preferable to use a constructor when creating an object and setting values ​​(entity must always be valid) and try to get rid of dependency injection through a constructor, like this:

public function updatePassword($plainPassword, PasswordEncoderInterface $encoder) {
    $this->password = $this->encoder->encode($plainPassword);
}

If we have many options for creating an object, it is convenient to use named constructors like this:
public static function create($userName, $email) {
    $user = new self();
    $user->username = $username;
    // ...
    return $user;
}

public static function createFromMyVariant($uid) {
    $user = new self();
    // ...
    return $user;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question