Skip to content

Instantly share code, notes, and snippets.

@kamekoopa
Created July 12, 2011 10:15
Show Gist options
  • Save kamekoopa/1077727 to your computer and use it in GitHub Desktop.
Save kamekoopa/1077727 to your computer and use it in GitHub Desktop.
traitをuseしたクラスを継承してみるテスト
<?php
trait Hello {
public function hello(){
echo "hello\n";
}
}
trait World {
public function world(){
echo "world\n";
}
}
trait HelloWorld {
use Hello, World;
public function helloWorld(){
$this->hello();
$this->world();
}
}
class Test {
use HelloWorld;
}
class TestChild extends Test{
}
$test = new TestChild();
$test->helloWorld();
$test->hello();
$test->world();
?>
/* 出力 親クラスでuseしたtraitは小クラスでも使える */
hello
world
hello
world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment