23. 継承の場合
interface Persion
{
public function getName();
}
class Employee
{
private $_name;
public function __construct( $name )
{
$this->_name = $name;
}
public function printName()
{
print($this->_name.PHP_EOL);
}
}
class EmployeePersion extends Employee implements Persion
{
public function __construct($name)
{
Employee::__construct($name);
}
public function getName (){
Employee::printName();
}
}
24. 継承の場合
$employee = new EmployeePersion("さいとう");
$employee->getname();
$employee->printName();
さいとう
さいとう
26. 委譲の場合
interface Persion
{
public function getName();
}
class Employee
{
private $_name;
public function __construct( $name )
{
$this->_name = $name;
}
public function printName()
{
print($this->_name.PHP_EOL);
}
}
class EmployeePersion implements Persion
{
private $_person;
public function __construct ( $name )
{
$this->_person = new Employee( $name );
}
public function getName (){
$this->_person->printName();
}
}
40. //Strategy interface
interface Strategy {
public function test();
}
// Strategyインスタンスを実行するContext
class Context {
private function __construct(){}
public static function test( Strategy $strategy) {
return $strategy->test();
}
}
//Strategy interfaceの実装
class Test2 implements Strategy {
public function test() {
return 2;
}
}
//Strategy interfaceの実装
class Test1 implements Strategy {
public function test() {
return 1;
}
}
41. 1
2
$val = Context::test( new Test1());
echo "$val n";
$val = Context::test( new Test2());
echo "$val n";