In this tutorial, I will explain what is Traits in PHP and how to use Traits in your PHP script.
Traits are a mechanism that allows you to create reusable code in PHP where multiple inheritances are not supported. To create a Traits we use keyword trait in the PHP script.
Below example explain how to use Traits in your PHP script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php trait users { function getUserType() { } function getUserDescription() { } function getUserDelete() { } } class ezcReflectionMethod extends ReflectionMethod { use users; } class ezcReflectionFunction extends ReflectionFunction { use users; } ?> |
Liked this tutorial? Do Like & share with your friends 🙂
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.