Answer the question
In order to leave comments, you need to log in
What are the advantages of ValueObject and DTO?
I looked at a bunch of materials about this pattern everywhere they explain the essence, but why I still don’t understand
it, let’s say let’s do it
class DateRange {
function __counstruct( $from, $to ) {
. . .
}
function getValue($name) {
if($name != "from" and $name != "to" ) {
throw new \Exception("");
}
return $this->{$name};
}
return $this->render($view,
array_map(function( $hit ) {
return $hit->getVersionInfo()->getContenInfo();
}, $searchResults);
Answer the question
In order to leave comments, you need to log in
The advantage of VO is that these are values that have already been checked for bounds and type. In your example, validation occurs in the getter - this is, to put it mildly, pointless. Here is an example for you
class UUID
{
/** @var string */
private $value;
/**
* @param string $value
*/
public function __construct(string $value)
{
if (!preg_match('/^[\da-f]{32}$/', $value)) {
throw new \InvalidArgumentException(
sprintf('Argument "$value" must be correct UUID, actual value: "%s"', $value)
);
}
$this->value = $value;
}
/**
* @return string
*/
public function getValue(): string
{
return $this->value;
}
}
class MyTypedCollection implements \Countable, \IteratorAggregate, \ArrayAccess
...
Well, the parameter to the function will need to be passed not two but one. there is no behavior
a key-value array is fineThe method leads to many errors, is more verbose, more expensive to maintain.
how to implement a typed collection of type in php as in SISo implement it - use type hinting in getters and setters. And validation for more complex situations.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question