<?php error_reporting(E_ALL); trait THello { public function a() { echo 'A'; } } class TraitsTest { use THello { a as b; } } $test = new TraitsTest(); $test->a(); $test->b();
<?php error_reporting(E_ALL); trait THello { public abstract function hello(); } trait THelloImpl { public function hello() { echo 'Hello'; } } class TraitsTest { use THello; use THelloImpl; } $test = new TraitsTest(); $test->hello();
--TEST-- Non-conflicting properties should work just fine. --FILE-- <?php error_reporting(E_ALL); trait THello1 { public $hello = "hello"; } trait THello2 { private $world = "World!"; } class TraitsTest { use THello1; use THello2; function test() { echo $this->hello . ' ' . $this->world; } } var_dump(property_exists('TraitsTest', 'hello')); var_dump(property_exists('TraitsTest', 'world')); $t = new TraitsTest(); $t->test(); ?> --EXPECTF-- bool(true) bool(true) hello World!
error_reporting(E_ALL); trait T1 { public function getText() { return $this->text; } } trait T2 { public function setTextT2($val) { $this->text = $val; } } class TraitsTest { use T1; use T2; private $text = 'test'; public function setText($val) { $this->text = $val; } } $o = new TraitsTest(); var_dump($o->getText()); $o->setText('foo'); var_dump($o->getText()); $o->setText('bar'); var_dump($o->getText());