1. Traits feature is added. Traits is used to reduce the limitations of single inheritance. A Trait cannot be instantiated on its own. Traits allows its methods to be reused in several independent classes. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php trait Hello { public function sayHello() { echo 'Hello '; } } trait World { public function sayWorld() { echo 'World'; } } class MyHelloWorld { use Hello, World; public function sayExclamationMark() { echo '!'; } } $o = new MyHelloWorld(); $o->sayHello(); $o->sayWorld(); $o->sayExclamationMark(); ?> |
In the above example code we define a trait using the keyword trait followed by its definition. We can … Continue Reading