Skip to content

Instantly share code, notes, and snippets.

@diloabininyeri
Created February 6, 2024 09:04
Show Gist options
  • Save diloabininyeri/dd7dc9e261e5e0ec8dfde673b407ffce to your computer and use it in GitHub Desktop.
Save diloabininyeri/dd7dc9e261e5e0ec8dfde673b407ffce to your computer and use it in GitHub Desktop.
Entity,ValueObject and Aggregate in the php(DDD)
<?php
class BookEntity
{
public function __construct(private int $id, private string $title)
{
}
public function getId(): int
{
return $this->id;
}
public function getTitle(): string
{
return $this->title;
}
}
readonly class BookValueObject
{
public function __construct(private string $author)
{
}
public function getAuthor(): string
{
return $this->author;
}
}
readonly class BookAggregator
{
public function __construct(private BookEntity $bookEntity, private BookValueObject $bookValueObject)
{
}
public function getBookDetails(): string
{
$bookDetails = "Book Title: {$this->bookEntity->getTitle()}";
$bookDetails .= "\nBook Author: {$this->bookValueObject->getAuthor()}";
return $bookDetails;
}
}
$bookAggregator = new BookAggregator(
new BookEntity(1, 'Domain-Driven Design'),
new BookValueObject('Eric Evans')
);
echo $bookAggregator->getBookDetails();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment