Last active
August 29, 2015 14:07
-
-
Save tarunama/54aae9776c7cefc3f5ae to your computer and use it in GitHub Desktop.
[PHP]抽象クラスとTraitのコードを見比べる。 ref: http://qiita.com/tarunama/items/694c790101eacf759500
This file contains 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 | |
abstract class Working | |
{ | |
abstract protected function where(); | |
abstract protected function why(); | |
abstract protected function how(); | |
public function work() | |
{ | |
echo $this->where($where); | |
echo $this->why($reason); | |
echo $this->how($how); | |
} | |
} | |
class Person extends Working | |
{ | |
protected function where() | |
{ | |
return '横浜で'; | |
} | |
protected function why() | |
{ | |
return 'お金欲しいから'; | |
} | |
protected function how() | |
{ | |
return '営業する'; | |
} | |
} | |
$bob = new Person(); | |
$bob->work(); // 横浜でお金欲しいから営業する | |
This file contains 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 | |
trait Working | |
{ | |
protected function where($where) | |
{ | |
return $where; | |
} | |
protected function why($reason) | |
{ | |
return $reason; | |
} | |
protected function how($how) | |
{ | |
return $how; | |
} | |
public function work($where, $reason, $how) | |
{ | |
echo $this->where($where); | |
echo $this->why($reason); | |
echo $this->how($how); | |
} | |
} | |
class Person | |
{ | |
use Working; | |
} | |
$bob = new Person(); | |
$where = '横浜で'; | |
$why = 'お金欲しいから'; | |
$reason = '営業する'; | |
$bob->work($where, $why, $reason); // 横浜でお金欲しいから営業する |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment