Skip to content

Instantly share code, notes, and snippets.

@tarunama
Last active August 29, 2015 14:07
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 tarunama/54aae9776c7cefc3f5ae to your computer and use it in GitHub Desktop.
Save tarunama/54aae9776c7cefc3f5ae to your computer and use it in GitHub Desktop.
[PHP]抽象クラスとTraitのコードを見比べる。 ref: http://qiita.com/tarunama/items/694c790101eacf759500
<?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(); // 横浜でお金欲しいから営業する
<?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