Created
July 10, 2014 03:18
-
-
Save whizark/07f883255b5970a940c4 to your computer and use it in GitHub Desktop.
Constructor trait #test #php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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