What are PHP traits?

Traits are a group of functions that can be included in other classes. For example,

trait A {
    public function test() {
        print 'HELLO';
    }
}

class B {
    use A;
}

class C {
    use A;
}

$b = new B();
$b->test(); // Will print 'Hello'.

$c = new C();
$c->test(); // Will print 'Hello'.

Leave a Reply