PHP OOP - Traits
PHP - What are Traits?
PHP only supports single inheritance: a child class can inherit only from one single parent.
Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).
Traits are declared with the trait keyword:
Syntax
To use a trait in a class, use the use keyword:
Syntax
<?php
class MyClass {
use TraitName;
}
?>
Let's look at an example:
PHP - Using Multiple Traits
Let's look at another example: