Skip to content

Instantly share code, notes, and snippets.

@rightgo09
Created August 18, 2015 03: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 rightgo09/7f96be27b0c4009b820f to your computer and use it in GitHub Desktop.
Save rightgo09/7f96be27b0c4009b820f to your computer and use it in GitHub Desktop.
module HogeTrait
def h
res = super
puts "HogeTrait::h() dayo"
res['t'] = 'traitdayo'
res
end
end
class ParentHoge
def h
puts "ParentHoge::h() dayo"
{h: 'hdayo'}
end
end
class ChildHoge < ParentHoge
include HogeTrait
def h
res = super
puts "ChildHoge::h() dayo"
res
end
end
h = ChildHoge.new
p h.h
#ParentHoge::h() dayo
#HogeTrait::h() dayo
#ChildHoge::h() dayo
#{:h=>"hdayo", "t"=>"traitdayo"}
<?php
trait HogeTrait {
public function h() {
$res = parent::h();
echo "HogeTrait::h() dayo\n";
$res['t'] = 'traitdayo';
return $res;
}
}
class ParentHoge {
public function h() {
echo "ParentHoge::h() dayo\n";
return ['h' => 'hdayo'];
}
}
class ChildHoge extends ParentHoge {
use HogeTrait { h as hh; }
public function h() {
$res = $this->hh();
echo "ChildHoge::h() dayo\n";
return $res;
}
}
$h = new ChildHoge();
var_dump($h->h());
//ParentHoge::h() dayo
//HogeTrait::h() dayo
//ChildHoge::h() dayo
//array(2) {
// 'h' =>
// string(5) "hdayo"
// 't' =>
// string(9) "traitdayo"
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment