D
D
d999992021-05-05 18:35:26
PHP
d99999, 2021-05-05 18:35:26

What is better Null or empty string in ValueObject?

Which is better to use string which will be in ValueObject or null?

public function __construct(Request $request)
    {
        $this->fullname = $request->request->get('fullname', null);


Whole constructor:
public function __construct(Request $request)
    {
        $contactInfo = $request->request->get('contactInfo');

        $this->fullname = $request->request->get('fullname', null);
        $this->email = $contactInfo['email'] ?? null;
        $this->phone = $contactInfo['phone'] ?? null;
        $this->linkedInUrl = $contactInfo['linkedInUrl'] ?? null;
        $this->currentCompany = $request->request->get('current_company', null);
        $this->jobId = $request->request->get('job_id', null);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MikUrrey, 2021-05-05
@MikUrrey

You receive and process GET parameters, a normal GET parameter is a string.
"In the right way" -

$this->fullname = $request->request->get('fullname', '');

Creating a NullObject in this case is overkill, PHP is one of the few PLs where a string is not an object. Null would also fit, but it's "not Orthodox".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question