Skip to content

Instantly share code, notes, and snippets.

@whizark
Created July 10, 2014 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whizark/07f883255b5970a940c4 to your computer and use it in GitHub Desktop.
Save whizark/07f883255b5970a940c4 to your computer and use it in GitHub Desktop.
Constructor trait #test #php
<?php
interface PersonInterface
{
public function getFullname();
}
trait PersonTrait
{
private $name;
public function getFullname()
{
return $this->name->getFullname();
}
}
// An aggregate root
trait AsianTrait
{
use PersonTrait;
public function __construct($lastName, $firstName)
{
$this->name = new Name($firstName, $lastName);
}
}
trait EuropeanTrait
{
use PersonTrait;
public function __construct($firstName, $lastName)
{
$this->name = new Name($firstName, $lastName);
}
}
// A value object
class Name
{
public function __construct($firstName, $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function getFullname()
{
return $this->firstName . ' ' . $this->lastName;
}
}
class Japanese implements PersonInterface
{
use AsianTrait;
}
$person = new Japanese('Yamada', 'Taro');
echo $person->getFullname() . PHP_EOL;
// Taro Yamada
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment