1
2<?php
3trait Hello {
4 public function sayHello() {
5 echo 'Hello ';
6 }
7}
8
9trait World {
10 public function sayWorld() {
11 echo 'World!';
12 }
13}
14
15trait HelloWorld {
16 use Hello, World;
17}
18
19class MyHelloWorld {
20 use HelloWorld;
21}
22
23$o = new MyHelloWorld();
24$o->sayHello();
25$o->sayWorld();
26?>
27
28 // L'exemple ci-dessus va afficher : Hello World !
29
1
2<?php
3class Base {
4 public function sayHello() {
5 echo 'Hello ';
6 }
7}
8
9trait SayWorld {
10 public function sayHello() {
11 parent::sayHello();
12 echo 'World!';
13 }
14}
15
16class MyHelloWorld extends Base {
17 use SayWorld;
18}
19
20$o = new MyHelloWorld();
21$o->sayHello();
22?>
23
24